Angularjs 如何管理角度过滤器中的舍入?
我正在开发一个角度滤波器,它可以将四舍五入到6位小数。当小数仅包含零时,我不知道如何舍入到2个小数。因此,1.00000将转换为1.00。 这就是我的过滤器的外观:Angularjs 如何管理角度过滤器中的舍入?,angularjs,Angularjs,我正在开发一个角度滤波器,它可以将四舍五入到6位小数。当小数仅包含零时,我不知道如何舍入到2个小数。因此,1.00000将转换为1.00。 这就是我的过滤器的外观: app.filter('customCurrency',function ($filter) { return function (amount, currencySymbol,fractionSize) { var currency = $filter('currency'); if (a
app.filter('customCurrency',function ($filter) {
return function (amount, currencySymbol,fractionSize) {
var currency = $filter('currency');
if (amount < 0) {
return currency(amount, currencySymbol).replace('(', '-').replace(')', '');
}
debugger;
if (fractionSize !== undefined) {
amount = currency(amount, currencySymbol, fractionSize);
} else {
amount = currency(amount, currencySymbol)
}
debugger;
var amounts = amount.split(".");
var amountHtml ;
if (amounts[1].length==2 && amounts[1][0]==0 && amounts[1][1]==0)
{
amountHtml = amounts[0] + '<span class="decimals">.00</span>';
}
else
{
amountHtml= amounts[0] + '<span class="decimals">.' + amounts[1] + '</span>';
}
return amountHtml;
};
});
app.filter('customCurrency',函数($filter){
返回函数(金额、货币符号、分形大小){
变量货币=$filter('currency');
如果(金额<0){
返回货币(金额,货币符号)。替换(“(”,“-”)。替换(“)”,”;
}
调试器;
如果(fractionSize!==未定义){
金额=货币(金额、货币符号、分位数);
}否则{
金额=货币(金额,货币符号)
}
调试器;
风险值金额=金额拆分(“.”);
var-amountHtml;
如果(金额[1]。长度==2和金额[1][0]==0和金额[1][1]==0)
{
amountHtml=金额[0]+'.00';
}
其他的
{
amountHtml=金额[0]+'.+金额[1]+'';
}
返回数量html;
};
});
plunkr:试试这个
it('if I have 3 zeros or more in decimals only display max of 2 zeros in decimals', function() {
// using $filter
expect($filter('customCurrency')(1.000, '$', 2)).toEqual('$1<span class="decimals">.00</span>');
});
it('如果我有3个零或更多的小数,则只显示最多2个零的小数',函数(){
//使用$filter
expect($filter('customCurrency')(1.000,'$',2)).toEqual('1.00');
});
我不确定从长远来看这是否对您有效,因为我确信有更好的方法来解决这一问题,但如果您想快速解决问题,请尝试以下方法:
app.filter('customCurrency',函数($filter){
返回函数(金额、货币符号、分形大小){
变量货币=$filter('currency');
如果(金额<0){
返回货币(金额,货币符号)。替换(“(”,“-”)。替换(“)”,”;
}
调试器;
如果(fractionSize!==未定义){
金额=货币(金额、货币符号、分位数);
}否则{
金额=货币(金额,货币符号)
}
调试器;
风险值金额=金额拆分(“.”),
分馏部分=金额[1],
小数=分数部分长度,
zerosFromRight=countZerosFromRight(分馏部分);
如果(零自右>2){
退货金额[0]+'.00';
}
返回金额[0]+'.+金额[1]+';
///////////////////////////////////
函数countZerosFromRight(str){
var len=str.length,
计数=0;
而(len--){
如果(str[len]=“0”){
计数++;
持续
}
打破
}
返回计数
}
};
});
编辑
重新思考,认为这样更好:
我已经添加了这个测试,也许您不需要这个功能,但我认为它更健壮一些
it('应该只删除最后一个非零数的零',函数(){
expect($filter('customCurrency')(1.004000,'$',6)).toEqual('1.004');
期望($filter('customCurrency')(1.4066000,'$',6)).toEqual('1.4066');
});
app.js
var app = angular.module('plunker', ['ngRoute']);
'use strict';
var app = angular.module('plunker');
app.filter('customCurrency',function ($filter) {
return function (amount, currencySymbol, fractionSize) {
var currency = $filter('currency');
if (amount < 0) {
return currency(amount, currencySymbol).replace('(', '-').replace(')', '');
}
debugger;
var rounded = round(amount, fractionSize),
currencyString = currency(rounded, currencySymbol, fractionSize),
amounts = currencyString.split("."),
integerPart = amounts[0],
fractionalPart = amounts[1] || false,
indexLastNonZero = indexLastNonZero(fractionalPart);
// if only zeros after decimal then remove all but 2
if(indexLastNonZero === -1){
return integerPart + '<span class="decimals">.00</span>';
}
// if zeros and other numbers after decimal remove all trailing zeros
if(indexLastNonZero > 1){
return integerPart + '<span class="decimals">.' + fractionalPart.slice(0, indexLastNonZero + 1) + '</span>';
}
return integerPart;
/////////////////////////////////////////////
function round(str, decimals){
var num = +str;
return num.toFixed(decimals);
}
function indexLastNonZero(str){
var len = str.length;
while(len--){
if(str[len] !== '0'){
return len;
}
}
return -1;
}
};
});
var-app=angular.module('plunker',['ngRoute']);
"严格使用",;
var app=角度模块('plunker');
app.filter('customCurrency',函数($filter){
返回函数(金额、货币符号、分形大小){
变量货币=$filter('currency');
如果(金额<0){
返回货币(金额,货币符号)。替换(“(”,“-”)。替换(“)”,”;
}
调试器;
var四舍五入=四舍五入(金额、分位数),
currencyString=货币(四舍五入、CurrencySymber、fractionSize),
金额=currencyString.split(“.”),
整数部分=金额[0],
分数部分=金额[1]| |假,
indexLastNonZero=indexLastNonZero(分馏部分);
//如果小数点后只有零,则删除除2以外的所有数字
if(indexLastNonZero==-1){
返回整数部分+'.00';
}
//如果小数点后有零和其他数字,则删除所有尾随的零
如果(indexLastNonZero>1){
返回integerPart+'.'.+partitionlpart.slice(0,indexLastNonZero+1)+'';
}
返回整数部分;
/////////////////////////////////////////////
函数四舍五入(str,小数){
var num=+str;
返回num.toFixed(小数);
}
函数indexLastNonZero(str){
var len=str.length;
而(len--){
如果(str[len]!='0'){
回程透镜;
}
}
返回-1;
}
};
});
我想你传递分数的方法是错误的。不是6次传递,而是2次传递。更新了第二个演示和代码。这就是你的目标吗?
it('should only remove the zeros up to the last non-zero number', function() {
expect($filter('customCurrency')(1.004000, '$', 6)).toEqual('$1<span class="decimals">.004</span>');
expect($filter('customCurrency')(1.4066000, '$', 6)).toEqual('$1<span class="decimals">.4066</span>');
});
var app = angular.module('plunker', ['ngRoute']);
'use strict';
var app = angular.module('plunker');
app.filter('customCurrency',function ($filter) {
return function (amount, currencySymbol, fractionSize) {
var currency = $filter('currency');
if (amount < 0) {
return currency(amount, currencySymbol).replace('(', '-').replace(')', '');
}
debugger;
var rounded = round(amount, fractionSize),
currencyString = currency(rounded, currencySymbol, fractionSize),
amounts = currencyString.split("."),
integerPart = amounts[0],
fractionalPart = amounts[1] || false,
indexLastNonZero = indexLastNonZero(fractionalPart);
// if only zeros after decimal then remove all but 2
if(indexLastNonZero === -1){
return integerPart + '<span class="decimals">.00</span>';
}
// if zeros and other numbers after decimal remove all trailing zeros
if(indexLastNonZero > 1){
return integerPart + '<span class="decimals">.' + fractionalPart.slice(0, indexLastNonZero + 1) + '</span>';
}
return integerPart;
/////////////////////////////////////////////
function round(str, decimals){
var num = +str;
return num.toFixed(decimals);
}
function indexLastNonZero(str){
var len = str.length;
while(len--){
if(str[len] !== '0'){
return len;
}
}
return -1;
}
};
});