javascript 数字到字 - jquery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7257575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Number to Word - jquery
提问by Ricardo Binns
Anyone know's a way to describe a cash value or a plugin who does that ?
任何人都知道一种描述现金价值的方法或一个插件吗?
exp:if i have $ 1.200.000,00 and the user hover()
the value:
description will be "one million two hundred"
exp:如果我有 $1.200.000,00 并且用户hover()
价值:描述将是“一百万两百”
Thanks!
谢谢!
回答by Rion Williams
The following page mentions a very simple implementation:
下一页提到了一个非常简单的实现:
which can perform the conversion by using this function:
可以使用此函数执行转换:
var words = toWords(num);
Using jQuery, you can wrap this inside of your hover function, as shown below:
使用 jQuery,您可以将其包装在您的悬停函数中,如下所示:
//This will add a tooltip upon hovering with the "word" value.
$('div').hover(function(){
$(this).attr('title',toWords($(this).text()));
});
回答by dciso
Should check the source of this example, with some modification you should be able to achieve this:
应该检查这个例子的来源,通过一些修改你应该能够实现这一点:
http://abhisanoujam.blogspot.com/2009/07/having-fun-with-jquery-numbers-to-words.html
http://abhisanoujam.blogspot.com/2009/07/have-fun-with-jquery-numbers-to-words.html
回答by Arnaud Leymet
Here is a Javascript solution that's both compact and working well:
这是一个既紧凑又运行良好的 Javascript 解决方案:
It can be used like this:
它可以像这样使用:
var words = toWords(num);
回答by Vijayalakshmi S M
Validation for text box allowing only one dot
验证文本框只允许一个点
if(!document.getElementById("per").value.match(/^[0-9]*\.?[0-9]*$/)) //"per" is the id text box
{
alert("Enter Correct Interest Rate");
return (false);
}
I hope this might help.
我希望这可能会有所帮助。
回答by Vijayalakshmi S M
you can add this code for number to word.if your field are coming dynamic.Just apply the class ".numtowordcls" on your input type
function getNumberToWord(thisObj){
var dataType=$(thisObj).attr("dir");
if(dataType!=null && dataType!="" ){
var value=$(thisObj).val();
if(dataType==='decimal' || dataType==='currency' || dataType==='number' || dataType==='emd' ){
if(value!=undefined && value.trim()!="" ){
if(isNaN(value)){
alert("Kindly Enter Valid Value For Filed Type '"+dataType+"'");
$(thisObj).val("");
$(thisObj).focus();
}else{
if(value!=undefined && value.trim()!=""){
if(!$.isNumeric(value)){
alert("Please Eneter Numeric value !!!");
$(this).val('');
return false;
}
var firstPart=value.toString().split(".")[0];
if(firstPart.length>14){
alert("Please Enter value upto 14 Digit !!!");
$(thisObj).val(value.substr(0,14));
$(thisObj).focus();
return false;
}
var secondPart=value.toString().split(".")[1];
if(secondPart!=undefined && secondPart!="" && secondPart.length>2){
alert("Decimal allowed upto 2 places !!!");
$(thisObj).val(value.substr(0,14));
$(thisObj).focus();
return false;
}
}
}
}
}
}
$(".numtowordcls").each(function(){
var id=$(this).attr("id");
var dirObj=$(this).attr("dir");
var dataArray=id.split("_");
var finalId=dataArray[0]+"_"+dataArray[1]+"_"+dirObj;
var fieldvalue=$("#"+finalId).val();
if(fieldvalue!=undefined && fieldvalue.trim()!=""){
var ntwidvalue=ntwidvalue+""+$(this).attr("id")+"~~~"+parseFloat(fieldvalue)+"~~~~";
}else{
$('#'+id).val("");
}
});
console.log("aaaa");
$('button').click(function(){
var input = $('input');
input.val('ntwidvalue');
input.trigger('input');
});
}