根据下面的模拟,温度应具有
小数点左边至少2位
精确到小数点后一位数字
例如:
31.105
应该是31.1
2
应该是02.0
cityDetails = {
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
答案 0 :(得分:0)
在此处查看有效的闪电战: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'}}