带有空格作为千位分隔符的自定义过滤器不起作用 [英] Custom filter with white space as thousands separator is not working
问题描述
我正在尝试编写一个自定义过滤器来过滤我的应用程序中 xxx xxx,xxx
格式的数字.
I am trying to write a custom filter to filter numbers on the format xxx xxx,xxx
in my application.
这是我目前写的函数:
var formatNumber = function(input, fractionSeparator, thousandsSeparator, fractionSize) {
fractionSeparator = fractionSeparator || ',';
thousandsSeparator = thousandsSeparator || ' ';
fractionSize = fractionSize || 3;
var output = '', parts = [];
input = input.toString();
parts = input.split(".");
output = parts[0].replace(/(\d{3})/g, ""+ thousandsSeparator +"$1").trim();
if(parts.length>1){
output += fractionSeparator;
output += parts[1].substring(0, fractionSize);
}
return output;
};
实际上这个函数对某些数字很有效,但在其他情况下会失败,例如:
Actually this function works perfectly for some numbers but fails in other cases, for example:
适用于以下数字:
- 对于
451.585478
,它打印451,585
. - 对于
154455451.58
,它打印154 455 451,58
.
- For
451.585478
it prints451,585
. - For
154455451.58
it prints154 455 451,58
.
以下示例失败:
对于
54455451.58
它打印54455 451,58
.
对于 55451.58
,它打印 55451,58
.
演示:
var formatNumber = function(input, fractionSeparator, thousandsSeparator, fractionSize) {
fractionSeparator = fractionSeparator || ',';
thousandsSeparator = thousandsSeparator || ' ';
fractionSize = fractionSize || 3;
var output = '',
parts = [];
input = input.toString();
parts = input.split(".");
output = parts[0].replace(/(\d{3})/g, "" + thousandsSeparator + "$1").trim();
if (parts.length > 1) {
output += fractionSeparator;
output += parts[1].substring(0, fractionSize);
}
return output;
};
console.log(formatNumber(154455451.58));
console.log(formatNumber(55451.58));
console.log(formatNumber(54455451.58));
我不知道这有什么问题.任何帮助将不胜感激.
I don't know what's wrong with it. Any help will be appreciated.
我终于让它工作了,问题出在我的正则表达式上,这是我用来格式化数字中千位的最后一个正则表达式:
I finally got it working, the problem was with my Regex, this is the final regex I used to format thousands in my number:
output = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeparator).trim();
这是最终代码:
var formatNumber = function(input, fractionSeparator, thousandsSeparator, fractionSize) {
fractionSeparator = fractionSeparator || ',';
thousandsSeparator = thousandsSeparator || ' ';
fractionSize = fractionSize || 3;
var output = '',
parts = [];
input = input.toString();
parts = input.split(".");
output = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeparator).trim();
if (parts.length > 1) {
output += fractionSeparator;
output += parts[1].substring(0, fractionSize);
}
return output;
};
console.log(formatNumber(154455451.58));
console.log(formatNumber(55451.58));
console.log(formatNumber(54455451.58));
推荐答案
问题出在这里:
output = parts[0].replace(/(\d{3})/g, ""+ thousandsSeparator +"$1").trim();
您在每 3 位数字之前插入一个空格(我会在括号之间标记匹配项):
You are inserting the space before every 3 digits (I'll mark matches between brackets):
"(123)(456)78" --> "( 123)( 456)78"
然后你正在修剪它 --> "123 45678"
Then you are trimming it --> "123 45678"
你的想法很好,但你必须从右到左开始,所以我会反转 parts[0]
和替换:
Your idea is nice, but you must do it starting from right to left so I will invert parts[0]
and the replacement:
parts[0] = parts[0].split('').reverse().join('');
"12345678" --> "87654321"
parts[0] = parts[0].replace(/(\d{3})/g, "$1" + thousandsSeparator).trim();
"(876)(543)21" --> "(876 )(543 )21"
output = parts[0].split('').reverse().join('');
"876 543 21" --> "12 345 678"
你想要的输出是什么.这是更新的片段:
Which is the output you want. Here is the updated snippet:
var formatNumber = function(input, fractionSeparator, thousandsSeparator, fractionSize) {
fractionSeparator = fractionSeparator || ',';
thousandsSeparator = thousandsSeparator || ' ';
fractionSize = fractionSize || 3;
var output = '',
parts = [];
input = input.toString();
parts = input.split(".");
parts[0] = parts[0].split('').reverse().join('');
parts[0] = parts[0].replace(/(\d{3})/g, "$1" + thousandsSeparator).trim();
output = parts[0].split('').reverse().join('');
if (parts.length > 1) {
output += fractionSeparator;
output += parts[1].substring(0, fractionSize);
}
return output;
};
console.log(formatNumber(154455451.58));
console.log(formatNumber(55451.58));
console.log(formatNumber(54455451.58));
这篇关于带有空格作为千位分隔符的自定义过滤器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!