2

I get the value 17.000, 17 thousands.

var pay = document.getElementbyId("name").InnerHTML;
alert(pay);

it shows "17.000". But if I do like this:

var f = (pay * 24);
alert(f);

it shows 408, not 408.000. It makes 17 * 24 not 17.000 * 24. How to work around this?

PlayHardGoPro
  • 2,791
  • 10
  • 51
  • 90
  • Are you sure it's actually 17000 and not 17 with 3 decimal places? Because it is acting more like the latter, as if the culture using . to indicate 1000 place holder might not be set. – taylonr Aug 11 '13 at 21:45
  • I just getting the value of a `

    ` tag. I'm trying to use `ParseFloat` but not working. Any idea?

    – PlayHardGoPro Aug 11 '13 at 21:46
  • "I'm trying to use ParseFloat but not working" I see no such code. – j08691 Aug 11 '13 at 21:48

1 Answers1

6

The period is the decimal point in the English language. If you want "17.000" to be treated as seventeen-thousand and not seventeen, you have to remove the period:

var pay = +document.getElementById("name").innerHTML.replace(/\./g, '');

The unary plus (+) at the beginning converts the resulting string into a number. This is not strictly necessary in your example but can avoid problems in the long run.

Using parseFloat exposes the same problem as implicit type conversion.

If you want to format the number (i.e. convert it to a string with thousand separators) again, have a look at How to print a number with commas as thousands separators in JavaScript. Just use a period instead of a comma.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143