简体   繁体   中英

Use Javascript .numeric function to allow only 2 decimal places

How do you use the .numeric function to only allow a specified number of decimal places? Below is the line of code I'm using:

 $('#Distance').numeric({ allow: "." });

I'm not clear which plugin you're using, but I'm guessing it's one of these two:

texotela.co.uk/code/jquery/numeric/
auroratrading.co.th/js/

For both of them, they do not have a config option to restrict the number of numeric characters after the decimal place marker.

There's another jQuery plugin called meio mask that should let you accomplish what you want, see (decimal demo): http://www.meiocodigo.com/projects/meiomask/#mm_demos

From a cursory glance at the page, you pick one of the 'masks' (in your case 'decimal-us') and assign it to the 'alt' attribute of an input element. After that you call the setMask method in your ready listener. Let me know if this works out for you!

if you have markup similar to

<input type="text" value="2.235323232"/>

try

$(":input").val(parseFloat($(":input").val()).toFixed(2));

http://jsfiddle.net/WYyt3/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM