Sop*_*ert 39 javascript string-formatting
我有一个向数字添加逗号的功能:
function commafy( num ) {
num.toString().replace( /\B(?=(?:\d{3})+)$/g, "," );
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,它不太喜欢小数.鉴于以下用法示例,扩展我的功能的最佳方法是什么?
commafy( "123" ) // "123"
commafy( "1234" ) // "1234"
// Don't add commas until 5 integer digits
commafy( "12345" ) // "12,345"
commafy( "1234567" ) // "1,234,567"
commafy( "12345.2" ) // "12,345.2"
commafy( "12345.6789" ) // "12,345.6789"
// Again, nothing until 5
commafy( ".123456" ) // ".123 456"
// Group with spaces (no leading digit)
commafy( "12345.6789012345678" ) // "12,345.678 901 234 567 8"
Run Code Online (Sandbox Code Playgroud)
大概最简单的方法是首先分割小数点(如果有的话).哪里最好去哪里?
Gho*_*toy 79
用'.'分成两部分.并单独格式化.
function commafy( num ) {
var str = num.toString().split('.');
if (str[0].length >= 5) {
str[0] = str[0].replace(/(\d)(?=(\d{3})+$)/g, '$1,');
}
if (str[1] && str[1].length >= 5) {
str[1] = str[1].replace(/(\d{3})/g, '$1 ');
}
return str.join('.');
}
Run Code Online (Sandbox Code Playgroud)
Ita*_*hav 24
就那么简单:
var theNumber = 3500;
theNumber.toLocaleString();
Run Code Online (Sandbox Code Playgroud)
Tom*_*ite 13
以下是我认为可能有用的两种简洁方法:
此方法可以将数字转换为具有语言敏感表示的字符串。它允许两个参数,即locales& options。这些参数可能有点令人困惑,有关更多详细信息,请参阅上面来自 MDN 的文档。
总之,你可以简单地使用如下:
console.log(
Number(1234567890.12).toLocaleString()
)
// log -> "1,234,567,890.12"
Run Code Online (Sandbox Code Playgroud)
如果您看到我的不同之处,因为我们忽略了两个参数,它将根据您的操作系统返回一个字符串。
我们为什么考虑这个?这
toLocaleString()有点令人困惑,并非所有浏览器都支持,toLocaleString()也会四舍五入,所以我们可以用另一种方式来做。
// The steps we follow are:
// 1. Converts a number(integer) to a string.
// 2. Reverses the string.
// 3. Replace the reversed string to a new string with the Regex
// 4. Reverses the new string to get what we want.
// This method is use to reverse a string.
function reverseString(str) {
return str.split("").reverse().join("");
}
/**
* @param {string | number}
*/
function groupDigital(num) {
const emptyStr = '';
const group_regex = /\d{3}/g;
// delete extra comma by regex replace.
const trimComma = str => str.replace(/^[,]+|[,]+$/g, emptyStr)
const str = num + emptyStr;
const [integer, decimal] = str.split('.')
const conversed = reverseString(integer);
const grouped = trimComma(reverseString(
conversed.replace(/\d{3}/g, match => `${match},`)
));
return !decimal ? grouped : `${grouped}.${decimal}`;
}
console.log(groupDigital(1234567890.1234)) // 1,234,567,890.1234
console.log(groupDigital(123456)) // 123,456
console.log(groupDigital("12.000000001")) // 12.000000001
Run Code Online (Sandbox Code Playgroud)
小智 6
最简单的方法:
var num = 1234567890,
result = num.toLocaleString() ;// result will equal to "1 234 567 890"
Run Code Online (Sandbox Code Playgroud)
var num = 1234567.890,
result = num.toLocaleString() + num.toString().slice(num.toString().indexOf('.')) // will equal to 1 234 567.890
Run Code Online (Sandbox Code Playgroud)
var num = 1234567.890123,
result = Number(num.toFixed(0)).toLocaleString() + '.' + Number(num.toString().slice(num.toString().indexOf('.')+1)).toLocaleString()
//will equal to 1 234 567.890 123
Run Code Online (Sandbox Code Playgroud)
如果你想要 ',' 而不是 ' ':
var num = 1234567.890123,
result = Number(num.toFixed(0)).toLocaleString().split(/\s/).join(',') + '.' + Number(num.toString().slice(num.toString().indexOf('.')+1)).toLocaleString()
//will equal to 1,234,567.890 123
Run Code Online (Sandbox Code Playgroud)
如果不起作用,请设置参数如:“toLocaleString('ru-RU')”参数“en-EN”,将按','而不是''分割数字
我的代码中使用的所有函数都是原生 JS 函数。您可以在 GOOGLE 或任何 JS 教程/书籍中找到它们