【发布时间】:2022-01-07 09:27:01
【问题描述】:
我有一个跟踪库存水平变化的表格,在大多数情况下,加法/减法是整数(-1、-2、+2、+3)等。但是,在某些情况下,+0.5、-0.25 可能需要。我当前的代码得到了正确的值,但是当涉及到小数时,它会减去或添加到最接近的整数 (+1/-1)。 oldValue 和 newValue 变量是从库存变化表中填充的,我正在显示库存变化的数量。
下面是计算函数-
formatDifference(change: StockChange) {
const difference = parseInt(change.newValue || '0', 10) - parseInt(change.oldValue || '0', 10);
return difference > 0 ? '+' + difference : difference;
}
HTML -
<td style="text-align: center;">
({{formatDifference(change)}})
<div class="stockLabel">
Difference
</div>
</td>
【问题讨论】:
-
最简单的答案是改用parseFloat,因为您不想解析int ????
-
那你需要控制要显示的小数位数。见toLocaleString 或者直接使用the pipe suggested by Juan Vicente
标签: html angular typescript