How to append an extra 'Zero' after decimal in Javascript

谁说我不能喝 提交于 2021-01-27 04:28:22

问题


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 
            }
}

回答1:


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.



来源:https://stackoverflow.com/questions/8034429/how-to-append-an-extra-zero-after-decimal-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!