3

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);
sampaioPT
  • 95
  • 1
  • 2
  • 9
  • 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). – Álvaro González Jun 17 '16 at 11:40
  • but the method toFixed returns a string and i want a float – sampaioPT Jun 17 '16 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. – Álvaro González Jun 17 '16 at 14:53
  • 1
    @sampaioPT, Refer http://stackoverflow.com/a/2283670/1746830 – Rayon Jun 18 '16 at 04:48
  • So it is impossible to do this output: Output (float) 1.42 | 5.5 | 4.23 Output wanted: 1.42 | 5.50 | 4.23 – sampaioPT Jun 20 '16 at 10:16
  • @sampaioPT I don't know how else to explain it... Imagine you have a JPEG file and you want a picture with a wooden frame to hang on the chimney. You can print the file and take the sheet of paper to the frame workshop but you cannot have a wooden frame in the JPEG file. – Álvaro González Jun 20 '16 at 10:47
  • 1
    Thanks for reply @ÁlvaroGonzález! Now i understand! (I owe you one beer ;) ) – sampaioPT Jun 20 '16 at 15:53
  • Possible duplicate of [parse float with two decimal places](https://stackoverflow.com/questions/4435170/parse-float-with-two-decimal-places) – aimme Jan 12 '18 at 16:49

3 Answers3

8

You have to call toFixed after parseFloat:

parseFloat(yourString).toFixed(2)
Andrei Zhytkevich
  • 8,039
  • 2
  • 31
  • 45
  • 2
    `var newString = ...` – Aprillion Jun 17 '16 at 11:34
  • 2
    when i use the method "toFixed" it returns a string -> http://www.w3schools.com/jsref/jsref_tofixed.asp and I want a float in the final variable. However thanks for your reply – sampaioPT Jun 17 '16 at 13:54
  • 1
    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. – Andrei Zhytkevich Jun 17 '16 at 14:39
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));
        }
      }
Hitesh Kumar
  • 3,508
  • 7
  • 40
  • 71
  • @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 – sampaioPT Jun 17 '16 at 14:04
1

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