receive two houses after the comma

2

I need to receive a cash value and am only getting a house after the comma. example 75.50 I get 7.5 and 7 I need to receive 7.00;

I'm using Jquery;

    
asked by anonymous 18.05.2017 / 22:11

3 answers

5

You can use .toFixed() , a method of numbers, which does two things:

  • sets the number of digits in the decimal part
  • transforms a string into string to keep the zeros

console.log((7.5).toFixed(2)); // dá 7.50
console.log((7).toFixed(2)); // dá 7.00
console.log((7.204).toFixed(2)); // dá 7.20

Note : If the number of decimal places after the point is greater than that reported, round the value, with [0 ... 4] being rounded down and [5 ... 9] rounded up.

    
18.05.2017 / 22:14
1

One way is by using the toPrecision method, which allows you to define how many digits in total (including the digits to the left and to the right of the decimal point) should be displayed.

var num = 7;

var result = num.toPrecision(3);

document.write (result);
    
18.05.2017 / 23:01
0

Use this jQuery Mask plugin to mask all types.

link

    
18.05.2017 / 22:14