首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义格式号TypeScript角5

自定义格式号TypeScript角5
EN

Stack Overflow用户
提问于 2019-11-15 18:33:12
回答 4查看 4.2K关注 0票数 3

我需要将数值​​格式化如下:如果一个数字没有十进制,请考虑这个值。如果您有任何小数位格式,在",“或”“之后有4位数字。

示例:

什么都不做 40 50 休假如下 4.4主要产品. 7.1再转轨7.10000 100.1再结晶100.10000 1000.2 000- 1000.20000

我试图创建一个@管道,但它没有按照我试图实现的代码运行。

代码语言:javascript
复制
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:

代码语言:javascript
复制
<td class="text-center">{{valueOrder | bigInteger: valueOrder > 100}}</td>

解决我的问题最好的办法是什么?

EN

回答 4

Stack Overflow用户

发布于 2019-11-15 18:42:23

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

代码语言:javascript
复制
let value = 1.2;

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

票数 1
EN

Stack Overflow用户

发布于 2019-11-15 20:26:19

您可以使用数字管道:

代码语言:javascript
复制
  <p *ngIf="num % 1 !== 0" >{{num | number:'1.4-4'}}</p>
  <p *ngIf="num % 1 === 0">{{mum | number:'1.0-4'}}</p>

并检查该数字是否具有十进制值,并根据所需格式显示。或者用一个漂亮的if elseng-template触摸

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

在线演示

票数 0
EN

Stack Overflow用户

发布于 2019-11-15 22:04:09

如果还想在coma值中追加0000,则必须将值的类型string,因为如果没有字符串值,则不能在值中添加0000<代码>E29。

代码语言:javascript
复制
// 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);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58882819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档