0

I tried to convert number to string in JavaScripts using toString() but it truncates insignificant zeros from numbers. For examples;

var n1 = 250.00 
var n2 = 599.0 
var n3 = 056.0 

n1.toString() // yields  250
n2.toString() // yields 599
n3.toString() // yields 56

but I dont want to truncate these insignificant zeros ( "250.00"). Could you please provide any suggestions?. Thank you for help.

2
  • Why do you need to truncate that zeros? Commented Nov 4, 2015 at 16:01
  • I dont want to Truncate. I want to convert as it is ( number = 250.00 to string = "250.00") Commented Nov 4, 2015 at 16:03

2 Answers 2

3

The number doesn't know how many trailing 0 there are because they are not stored. In math, 250, 250.00 or 250.0000000000000 are all the same number and are all represented the same way in memory.

So in short, there is no way to do what you want. What you can do is format all numbers in a specific way. See Formatting a number with exactly two decimals in JavaScript.

Sign up to request clarification or add additional context in comments.

Comments

0

As far as I know, you can't store number with floating zeros, but you can create zeroes with floating zeroes, by using toFixed:

var n1 = 250;
var floatedN1 = n1.toFixed(2); //type 'string' value '250.00'

5 Comments

10X, I fixed my mistake
If I understand correct he don't need fixed decimals, he need to leave them as is. So 599.0 should be remain 599.0, not 599.00.
So where he get the number he should store it as string, or keep somewhere how many zeroes he want at each point, and he can change the argument in the function to any number he want. for 599.0 he should use toFixed(1)
In my case , 250, 250.0 and 250.00 have different meanings
@Aarav: Then you have to store string values in the first place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.