gpt4 book ai didi

html - 自定义格式数字 TypeScript Angular 5

转载 作者:行者123 更新时间:2023-12-04 17:31:49 25 4
gpt4 key购买 nike

我需要格式化数值如下:如果数字没有小数位,请考虑该值。如果您有任何小数位格式,在“,”或“。”之后有 4 位数字

例子:

Do nothing
40
50

Leave as follows
4.4       | 4,40000
7.1       | 7.10000
100.1   | 100.10000
1000.2 | 1000.20000

我尝试创建一个@Pipe 但没有成功遵循我尝试实现的代码

import { Pipe, PipeTransform } from '@angular/core';
import { DecimalPipe } from '@angular/common'

@Pipe({
name: 'bigInteger'
})
export class BigInteger extends DecimalPipe implements PipeTransform {
transform(value: any, args?: any): any {
const format = args[0] ? '1' : '1.4-4';
let result;
result = super.transform(value, format).toString().replace(",",".");
console.log(result);
return result;
}

}

HTML:

<td class="text-center">{{valueOrder | bigInteger: valueOrder > 100}}</td>

我的问题的最佳解决方案是什么?

最佳答案

您可以使用 toFixed() 方法在这里完成您需要的操作。

let value = 1.2;

value = value.toFixed(5); // 5 is the number of digits after the decimal
console.log(value);

关于html - 自定义格式数字 TypeScript Angular 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58882819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com