简体   繁体   English

自定义格式编号 TypeScript Angular 5

[英]Custom Format Number TypeScript Angular 5

I need to format numeric values as follows: If a number has no decimal places consider the value.我需要按如下方式格式化数值:如果数字没有小数位,请考虑该值。 If you have any decimal place format with 4 digits after the "," or "."如果您有任何小数位格式,在“,”或“。”之后有 4 位数字。

Examples:例子:

Do nothing没做什么
40 40
50 50

Leave as follows离开如下
4.4 | 4.4 | 4,40000 4,40000
7.1 | 7.1 | 7.10000 7.10000
100.1 | 100.1 | 100.10000 100.10000
1000.2 | 1000.2 | 1000.20000 1000.20000

I tried to create an @Pipe But it didn't work out Follows the code I tried to implement我试图创建一个@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: Html:

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

What is the best solution to my problem?我的问题的最佳解决方案是什么?

You may use the toFixed() method to do what you need here.您可以使用toFixed()方法在此处执行您需要的操作。

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

You could use the number pipe:您可以使用编号 pipe:

  <p *ngIf="num % 1 !== 0" >{{num | number:'1.4-4'}}</p>
  <p *ngIf="num % 1 === 0">{{mum | number:'1.0-4'}}</p>

And check whether or not the number has a decimal values and display based on the format desired.并检查数字是否具有十进制值并根据所需格式显示。 Or with a nice ng-template and the if else touch或者使用一个不错的ng-templateif else touch

<div>
    <p *ngIf="num%1!== 0; else noDecimals" >{{num | number:'1.4-4'}}</p>
    <ng-template #noDecimals >decimals<p>{{num }}</p></ng-template>
  </div>

Online Demo 在线演示

If you want to append 0000 in coma values also, it is necessary that the type of your values should be of string , because without string values you cannot append 0000 in the values.如果您还想在逗号值中使用 append 0000 ,则值的类型必须是string ,因为没有字符串值,您不能在值中使用 append 0000

 // 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);

Thanks I resolved as way谢谢我解决了

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

@Pipe({
  name: 'numberPipe'
})
export class DecimalNumberPipe extends DecimalPipe implements PipeTransform {
    transform(value: any): any {
    let result;
    if(value % 1 !== 0) {
     result = value.toFixed(4);
    } else if(value % 1 === 0){
      result = value;
    }
   return result;
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM