英文:
How to prevent auto deleting 0 after .?
问题
I want to display $125.00, but instead I get $125, how to prevent it
import { Typography } from "@mui/material";
function ProductPrize({}) {
const presentPrize = 125.0;
return (
<Typography variant="h4" sx={{ fontWeight: "bold" }}>
{`$${presentPrize}`}
</Typography>
);
}
export default ProductPrize;
I tried parseFloat etc.
英文:
I want to display $125.00, but insted I get $125, how to prevent it
import { Typography } from "@mui/material";
function ProductPrize({}) {
const presentPrize = 125.0;
return (
<Typography variant="h4" sx={{ fontWeight: "bold" }}>
{`$${presentPrize}`}
</Typography>
);
}
export default ProductPrize;
I tried parsefloat etc.
答案1
得分: 1
JavaScript会打印出它所需的最少小数位数。如果你总是想要保留2位小数,你可以将代码更改为以下内容:
<Typography variant="h4" sx={{ fontWeight: "bold" }}>
{`$${(Math.round(presentPrize * 100) / 100).toFixed(2)}`}
</Typography>
这将四舍五入1.236为$1.24
。
英文:
JavaScript prints out the minimum number of decimal points it needs to. If you always want 2 decimal points, you can change the code to the following:
<Typography variant="h4" sx={{ fontWeight: "bold" }}>
{`$${(Math.round(presentPrize * 100) / 100).toFixed(2)}`}
</Typography>
This with round 1.236 to $1.24
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论