4

I need to round a value with 2 decimal places that I receive from a web service. To do this i use this methods toFixed and parseFloat because I need the final value in a float. However when I have this value "5.5000000" he shows me the value with only one decimal place "5.5"...

I did in that way:

var num = 5.5000000; var n = num.toFixed(2); var numFinal = parseFloat(n); 
8
  • If you have a number and want a string you don't need parseFloat() because that function does the exact opposite (convert from string to number). Commented Jun 17, 2016 at 11:40
  • but the method toFixed returns a string and i want a float Commented Jun 17, 2016 at 13:55
  • "5.5000000" is just how the float happens to be displayed when you print it. There's no direct translation between the internal base 2 representation of a floating point number and decimals in a given base 10 representation. Commented Jun 17, 2016 at 14:53
  • 1
    @sampaioPT, Refer stackoverflow.com/a/2283670/1746830 Commented Jun 18, 2016 at 4:48
  • 1
    Thanks for reply @ÁlvaroGonzález! Now i understand! (I owe you one beer ;) ) Commented Jun 20, 2016 at 15:53

3 Answers 3

8

You have to call toFixed after parseFloat:

parseFloat(yourString).toFixed(2) 
Sign up to request clarification or add additional context in comments.

3 Comments

var newString = ...
when i use the method "toFixed" it returns a string -> w3schools.com/jsref/jsref_tofixed.asp and I want a float in the final variable. However thanks for your reply
Having float with 2 decimal places is formatting the float value, i.e. converting it to a string. If you want to use the float value itself you don't need to strip away the rest of the float.
2

Short Answer: There is no way in JS to have Number datatype value with trailing zeros after a decimal.

Long Answer: Its the property of toFixed or toPrecision function of JavaScript, to return the String. The reason for this is that the Number datatype cannot have value like a = 2.00, it will always remove the trailing zeros after the decimal, This is the inbuilt property of Number Datatype. So to achieve the above in JS we have 2 options

  1. Either use data as a string or
  2. Agree to have truncated value with case '0' at the end ex 2.50 -> 2.5. Number Cannot have trailing zeros after decimal

Comments

1

You can do something like this

 /** * Convert the value to the Number with the precision. * @param {Number} value value to be converted * @param {Number} [precision=0] number of decimal places * @returns {Number} converted number. */ function toNumber (value, precision) { precision = precision || 0; if (precision === 0) { return value * 1; } else { return Number((value * 1).toFixed(precision)); } } 

1 Comment

@Hitesk Kumar thanks for your reply. However when i have this value 5.5000004 the output it is 5.5 only with one decimal places and i want with 2 decimal places like this 5.50

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.