0

I need floating number output exactly like number_format function in php.

Javascript Code

Math.round(totalCredit).toFixed(2)

PHP Code

echo number_format(22212 , 2);

above code return me 22,212.00 and this is exactly I want in js.

Expected output

22.212

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Peter
  • 777
  • 2
  • 13
  • 34
  • 1
    Why would you get `22.212` when you're rounding to 2 decimal places? Also, what is the type of `totalCredit`? You may need to use `parseFloat()` on it first, if it's a string – Rory McCrossan May 18 '17 at 09:08
  • using about math.round when I enter 22212 in input field it return me 22212.00. – Peter May 18 '17 at 09:10
  • Well, yes... `22212` is a whole number with no decimal places. – Rory McCrossan May 18 '17 at 09:11
  • @RoryMcCrossan echo number_format(22212 , 2); above code return me 22,212.00 and this is exactly I want in js. – Peter May 18 '17 at 09:13
  • Well now I'm confused. Your question says you expect `22.212` as the output...? Can you edit the question so it's clear what you're trying to do here – Rory McCrossan May 18 '17 at 09:15
  • If you want your number with commas, don't know why you are rounding first, so remove that, do the `.fixed` and then you can use the answer in [this post](http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript) to add commas. Your expected output in the yellow above should be deleted as it is different to what you put in the line above and is confusing – Pete May 18 '17 at 09:22

1 Answers1

0

Here is a good solution:

http://locutus.io/php/strings/number_format/

Please, stop thinking, jQuery is made for solving any of your JS problems. This problem is not jQuery related, jQuery is mostly (not only) for DOM related stuff

.

Alexander_F
  • 2,831
  • 3
  • 28
  • 61