根据下面的模拟,温度应该有小数点左边最少两位数小数点右边正好 1 位例如:31.105 应该 31.12 应该 02.0cityDetails = { city: "chennai", country: "india", weather: [ { date: "Aug 3, 2018", temperature: 2, weather_name: "sunny", weather_image: "some image url" } ] };下面是我试过的代码, transform(cityDetails) { this.temp = cityDetails.weather[0].temperature.toString().replace( /\d+/g, function pad(digits) { return digits.length === 1 ? '0' + digits : digits; }); }但它没有按预期工作。链接到我的https://stackblitz.com/edit/angular-wc8vu1
1 回答
幕布斯7119047
TA贡献1794条经验 获得超8个赞
在这里查看工作闪电战:https : //stackblitz.com/edit/angular-8dhh12? file = src/app/ app.component.ts
transform(cityDetails) {
let pad = (digits) => digits.split('.')[0].length === 1 ? '0' + digits : digits;
this.temp = pad(cityDetails.weather[0].temperature.toFixed(1));
}
使用number管道的更好方法是https://stackblitz.com/edit/angular-kzkne3?file=src/app/app.component.html
{{temp | number:'2.1'}}
添加回答
举报
0/150
提交
取消