如何在Javascript中在小数点后附加一个额外的“零" [英] How to append an extra 'Zero' after decimal in Javascript

查看:82
本文介绍了如何在Javascript中在小数点后附加一个额外的“零"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Iam是javascript的新手,可以使用一个文本框验证十进制数字.示例格式应为66,00.但是,如果用户键入66,0且在逗号后未键入两个零,则在离开文本框后应将其自动附加到该文本框.这样它才是正确的格式.我怎样才能得到这个.我如何追加?这是我的代码段.

 function check2(sender){
           var error = false;
           var regex = '^[0-9][0-9],[0-9][0-9]$';
           var v = $(sender).val();
           var index = v.indexOf(',');
           var characterToTest = v.charAt(index + 1);
           var nextCharAfterComma = v.charAt(index + 2);

            if (characterToTest == '0') { 

              //here need to add 
            }
}

解决方案

使用.toFixed(2)

阅读本文: http://www.javascriptkit.com/javatutors/formatnumber.shtml

|编辑|如果用户输入的小数太多,这也可以解决此问题.最好采用这种方式,而不要用逗号检查逗号后的每个数字.

Hye, Iam new to javascript working with one textbox validation for decimal numbers . Example format should be 66,00 .but if user type 66,0 and dont type two zero after comma then after leaving text box it should automatically append to it .so that it would be correct format of it . How can i get this .How can i append ?? here is my code snippet.

 function check2(sender){
           var error = false;
           var regex = '^[0-9][0-9],[0-9][0-9]$';
           var v = $(sender).val();
           var index = v.indexOf(',');
           var characterToTest = v.charAt(index + 1);
           var nextCharAfterComma = v.charAt(index + 2);

            if (characterToTest == '0') { 

              //here need to add 
            }
}

解决方案

Use .toFixed(2)

Read this article: http://www.javascriptkit.com/javatutors/formatnumber.shtml

|EDIT| This will also fix the issue if a user types in too many decimals. Better to do it this way, rather than having a if to check each digit after the comma.

这篇关于如何在Javascript中在小数点后附加一个额外的“零"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆