jQuery 使用 Javascript 以印度格式显示数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16037165/
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
Displaying a number in Indian format using Javascript
提问by tilak
I have the following code to display in Indian numbering system.
我有以下代码要显示在印度编号系统中。
var x=125465778;
var res= x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
Am getting this output :125,465,778
.
我得到这个输出:125,465,778
。
I need output like this: 12,54,65,778
.
我需要这样的输出:12,54,65,778
。
Please help me to sort out this problem .
请帮我解决这个问题。
回答by Prasath K
For Integers:
对于整数:
var x=12345678;
x=x.toString();
var lastThree = x.substring(x.length-3);
var otherNumbers = x.substring(0,x.length-3);
if(otherNumbers != '')
lastThree = ',' + lastThree;
var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
alert(res);
For float:
对于浮动:
var x=12345652457.557;
x=x.toString();
var afterPoint = '';
if(x.indexOf('.') > 0)
afterPoint = x.substring(x.indexOf('.'),x.length);
x = Math.floor(x);
x=x.toString();
var lastThree = x.substring(x.length-3);
var otherNumbers = x.substring(0,x.length-3);
if(otherNumbers != '')
lastThree = ',' + lastThree;
var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree + afterPoint;
alert(res);
回答by Tushar Gupta - curioustushar
i'm late but i guess this will help :)
我迟到了,但我想这会有所帮助:)
you can use Number.prototype.toLocaleString()
你可以使用Number.prototype.toLocaleString()
Syntax
句法
numObj.toLocaleString([locales [, options]])
var number = 123456.789;
// India uses thousands/lakh/crore separators
document.getElementById('result').innerHTML = number.toLocaleString('en-IN');
// → 1,23,456.789
document.getElementById('result1').innerHTML = number.toLocaleString('en-IN', {
maximumFractionDigits: 2,
style: 'currency',
currency: 'INR'
});
// → Rs.123,456.79
<div id="result"></div>
<div id="result1"></div>
回答by slopen
For integers only no additional manipulations needed.
对于整数,不需要额外的操作。
This will match every digit from the end, having 1 or more double digits pattern after, and replace it with itself + ",":
这将匹配末尾的每个数字,后面有 1 个或多个双位数模式,并将其替换为自身 + ",":
"125465778".replace(/(\d)(?=(\d\d)+$)/g, ",");
-> "1,25,46,57,78"
But since we want to have 3 in the end, let's state this explicitly by adding extra "\d" before match end of input:
但由于我们希望最后有 3,让我们通过在输入匹配结束之前添加额外的“\d”来明确说明这一点:
"125465778".replace(/(\d)(?=(\d\d)+\d$)/g, ",");
-> "12,54,65,778"
回答by Mohideen bin Mohammed
Simple way to do,
做法简单,
1. Direct Method using LocalString()
1. 使用 LocalString() 的直接方法
(1000.03).toLocaleString()
(1000.03).toLocaleString('en-IN') # number followed by method
2. using Intl - Internationalization API
2. 使用 Intl - 国际化 API
The Intlobject is the namespace for the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, and date and time formatting.
Intl对象是 ECMAScript 国际化 API 的命名空间,它提供语言敏感的字符串比较、数字格式以及日期和时间格式。
eg: Intl.NumberFormat('en-IN').format(1000)
例如: Intl.NumberFormat('en-IN').format(1000)
3. Using Custom Function:
3. 使用自定义函数:
function numberWithCommas(x) {
return x.toString().split('.')[0].length > 3 ? x.toString().substring(0,x.toString().split('.')[0].length-3).replace(/\B(?=(\d{2})+(?!\d))/g, ",") + "," + x.toString().substring(x.toString().split('.')[0].length-3): x.toString();
}
console.log("0 in indian format", numberWithCommas(0));
console.log("10 in indian format", numberWithCommas(10));
console.log("1000.15 in indian format", numberWithCommas(1000.15));
console.log("15123.32 in indian format", numberWithCommas(15123.32));
if your input is 10000.5,
如果您的输入是 10000.5,
numberWithCommas(10000.5)
You will get output like this, 10,000.5
你会得到这样的输出, 10,000.5
回答by D P Venkatesh
Given a number to below function, it returns formatted number in Indian format of digit grouping.
给下面的函数一个数字,它以数字分组的印度格式返回格式化的数字。
ex: input: 12345678567545.122343
output: 1,23,45,67,85,67,545.122343
例如:输入:12345678567545.122343
输出:1,23,45,67,85,67,545.122343
function formatNumber(num) {
input = num;
var n1, n2;
num = num + '' || '';
// works for integer and floating as well
n1 = num.split('.');
n2 = n1[1] || null;
n1 = n1[0].replace(/(\d)(?=(\d\d)+\d$)/g, ",");
num = n2 ? n1 + '.' + n2 : n1;
console.log("Input:",input)
console.log("Output:",num)
return num;
}
formatNumber(prompt("Enter Number",1234567))
回答by Nikhil
This should work.
这应该有效。
var number=12345678;
alert(number.toLocaleString());
you can also pass the arguments inside the function here by defualt it will take international convention. If you wants to use indian convention then u should write it like this.
您还可以通过默认方式在此处传递函数内部的参数,它将采用国际惯例。如果你想使用印度约定,那么你应该这样写。
alert(number.toLocaleString("hi-IN"));
But this code will work only on Chrome, Mozzilla and IE. It won't work on Safari.
但此代码仅适用于 Chrome、Mozzilla 和 IE。它不适用于 Safari。
回答by Vaibhav Khullar
Simply use https://osrec.github.io/currencyFormatter.js/
只需使用https://osrec.github.io/currencyFormatter.js/
Then all you need is:
那么你只需要:
OSREC.CurrencyFormatter.format(2534234, { currency: 'INR' });
// Returns ? 25,34,234.00
回答by shubhamkes
I am little late in the game. But here is the implicit way to do this.
我在比赛中有点晚了。但这是执行此操作的隐式方法。
var number = 3493423.34;
console.log(new Intl.NumberFormat('en-IN', { style: "currency", currency: "INR" }).format(number));
if you dont want currency symbol, use it like this
如果您不想要货币符号,请像这样使用它
console.log(new Intl.NumberFormat('en-IN').format(number));
回答by Yatender Singh
This function can handle float value properly just addition to another answer
此函数可以正确处理浮点值,只是添加到另一个答案
function convertNumber(num) {
var n1, n2;
num = num + '' || '';
n1 = num.split('.');
n2 = n1[1] || null;
n1 = n1[0].replace(/(\d)(?=(\d\d)+\d$)/g, ",");
num = n2 ? n1 + '.' + n2 : n1;
n1 = num.split('.');
n2 = (n1[1]) || null;
if (n2 !== null) {
if (n2.length <= 1) {
n2 = n2 + '0';
} else {
n2 = n2.substring(0, 2);
}
}
num = n2 ? n1[0] + '.' + n2 : n1[0];
return num;
}
this function will convert all function to float as it is
此函数会将所有函数按原样转换为浮点数
function formatAndConvertToFloatFormat(num) {
var n1, n2;
num = num + '' || '';
n1 = num.split('.');
if (n1[1] != null){
if (n1[1] <= 9) {
n2 = n1[1]+'0';
} else {
n2 = n1[1]
}
} else {
n2 = '00';
}
n1 = n1[0].replace(/(\d)(?=(\d\d)+\d$)/g, ",");
return n1 + '.' + n2;
}