如何防止小数点后的零被自动删除?

huangapple go评论66阅读模式
英文:

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 &quot;@mui/material&quot;;

function ProductPrize({}) {
  const presentPrize = 125.0;
  return (
    &lt;Typography variant=&quot;h4&quot; sx={{ fontWeight: &quot;bold&quot; }}&gt;
      {`$${presentPrize}`}
    &lt;/Typography&gt;
  );
}

export default ProductPrize;

I tried parsefloat etc.

答案1

得分: 1

JavaScript会打印出它所需的最少小数位数。如果你总是想要保留2位小数,你可以将代码更改为以下内容:

&lt;Typography variant=&quot;h4&quot; sx={{ fontWeight: &quot;bold&quot; }}&gt;
  {`$${(Math.round(presentPrize * 100) / 100).toFixed(2)}`}
&lt;/Typography&gt;

这将四舍五入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:

&lt;Typography variant=&quot;h4&quot; sx={{ fontWeight: &quot;bold&quot; }}&gt;
  {`$${(Math.round(presentPrize * 100) / 100).toFixed(2)}`}
&lt;/Typography&gt;

This with round 1.236 to $1.24

huangapple
  • 本文由 发表于 2023年2月10日 16:41:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定