我需要将数值格式化如下:如果一个数字没有十进制,请考虑这个值。如果您有任何小数位格式,在",“或”“之后有4位数字。
示例:
什么都不做 40 50 休假如下 4.4主要产品. 7.1再转轨7.10000 100.1再结晶100.10000 1000.2 000- 1000.20000
我试图创建一个@管道,但它没有按照我试图实现的代码运行。
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>解决我的问题最好的办法是什么?
发布于 2019-11-15 18:42:23
您可以使用toFixed()方法来完成您在这里需要的工作。
let value = 1.2;
value = value.toFixed(5); // 5 is the number of digits after the decimal
console.log(value);
发布于 2019-11-15 20:26:19
您可以使用数字管道:
<p *ngIf="num % 1 !== 0" >{{num | number:'1.4-4'}}</p>
<p *ngIf="num % 1 === 0">{{mum | number:'1.0-4'}}</p>并检查该数字是否具有十进制值,并根据所需格式显示。或者用一个漂亮的if else和ng-template触摸
<div>
<p *ngIf="num%1!== 0; else noDecimals" >{{num | number:'1.4-4'}}</p>
<ng-template #noDecimals >decimals<p>{{num }}</p></ng-template>
</div>发布于 2019-11-15 22:04:09
如果还想在coma值中追加0000,则必须将值的类型为string,因为如果没有字符串值,则不能在值中添加0000<代码>E29。
// my array with the numeric values in string format
let arr=["10", "20", "5.2", "6,7"];
// function which check the value and append 0000
function letsCheckAndChange(arr) {
arr.map((value, index) =>{
if(value.includes(",")) { // to check the coma
arr[index] = value + "0000"
} else {
if(parseInt(value) % 2 != 0) {
arr[index] = value.toString() + "0000";
}
}
})
}
letsCheckAndChange(arr);
document.write(arr);
https://stackoverflow.com/questions/58882819
复制相似问题