1

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?

3
  • 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. Commented Aug 11, 2013 at 21:45
  • I just getting the value of a <p> tag. I'm trying to use ParseFloat but not working. Any idea? Commented Aug 11, 2013 at 21:46
  • 1
    "I'm trying to use ParseFloat but not working" I see no such code. Commented Aug 11, 2013 at 21:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Does the + transform it into an number?
Understood, thanks @FelixKling! Isn't there a way to show the period? ;x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.