Zbyszek Kisły
Zbyszek Kisły

Reputation: 2238

On setting float value in number input with jQuery, the number has comma and I want a dot

Here's my code:

<input type="number" id='n'></input>
<button id="set">Set value</button>

$("#set").on("click", function() {
    $("#n").val("1.4");
});

here's fiddle: http://jsfiddle.net/kcbc701c/9/

When I set the input with jQuery I see "1,4" and my client wants to see "1.4". Is there a way to achieve it without changing the input field to "text"?

Upvotes: 0

Views: 374

Answers (1)

samanime
samanime

Reputation: 26537

The reason likely has to do with browser default language.

In some countries, using a dot for a decimal (e.g., 1.4) is the norm. In other countries, using a comma for a decimal is the norm (e.g., 1,4).

In the case of <input type="number" />, the browser is going to use whatever makes sense for the current user's browser.

So, you have a couple of ways to deal with this:

  • Specifically set the language in the <html> tag or directly on the <input> tags. You can use something like <input type="number" lang="en-150" />
  • Stop using type="number". While this isn't a great approach, if you treat it like a normal input and handle the logic yourself, there is no guesswork or possible browser incompatibility.

This blog has a really good write-up on this if you want more information: HTML5 number inputs

Upvotes: 2

Related Questions