receive two houses after the comma

Asked

Viewed 1,302 times

2

I need to get a cash amount and I’m only getting one house after the comma. example 75.50 I get 7.5 and 7 I need to receive 7.00;

I am using Jquery;

  • How do you "receive"? From a query? From the form?

3 answers

5


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

  • sets the number of digits in the decimal part
  • turns a number into string for the zeroes to hold

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 decimals after the point is higher than the reported one, round the value by being [0...4] rounded down and [5...9] rounded up.

  • 2

    I added a note on when the value exceeds the value of decimals, I do not know if it became good rs, but I found it interesting to add, in case you want to change or reverse is no problem :)

  • @Marceloboni great, thank you!

  • 1

    Thank you! @Sergio

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 right of the decimal point) must be displayed.

var num = 7;

var result = num.toPrecision(3);

document.write (result);

0

  • Thanks for the comment friend, I used jquery Mask in another project, but this was not possible, because in the input (input) were integers and in the calculation there were real numbers, often the result is a real number.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.