Mathew P. Jones
Mathew P. Jones

Reputation: 853

How to keep '.00'

How to keep '.00' in Javascript? I currently have the string "123456789012.00";
I want to get the double 123456789012.00 to keep .00

How can I do this?

Upvotes: 1

Views: 2308

Answers (2)

ndequeker
ndequeker

Reputation: 7990

You can use the toFixed() method to do this. The code below will log the result you want in the console of your browser.

var num = "123456789012.00";
console.log(parseFloat(num).toFixed(2));

Upvotes: 1

Roy Dictus
Roy Dictus

Reputation: 33139

A float uses the precision it needs (that's why it's called a "float" -- as in "floating point", the point has no fixed position).

If you want to display a float with the 2 significant digits (i.e. 2 digits after the point), you can use toFixed(2). That will not change the number, but will store it in a string with the number of digits you want to show.

Upvotes: 8

Related Questions