import { useState, useEffect } from “react”; import { FormatPriceProps } from…
- User
import { useState, useEffect } from “react”; import { FormatPriceProps } from “./FormatPrice.props”; import { useIntl, FormattedNumber } from “react-intl”;
const FormatPrice = ({ amount, currency_iso_code, dec }: FormatPriceProps) => { const intl = useIntl(); return ( <FormattedNumber value={amount} style={dec || “currency”} currency={currency_iso_code} minimumFractionDigits={0} maximumFractionDigits={Math.abs(amount) > 10 ? 2 : 4} {…intl.formatNumber()} /> ); };
const YourComponent = () => { const [summ, setSumm] = useState(0);
useEffect(() => { const formattedPrice = formatPrice((balanceUsdt / coin?.price) * (percent / 100)); setSumm(formattedPrice); }, [priceSymbol, percent]);
const formatPrice = (value: number) => { return value.toLocaleString(“en-US”, { style: “decimal” }); };
return ( <TextField type=“number” InputProps={{ disableUnderline: true }} variant=“standard” value={summ} sx={{ “& .MuiInputBase-input”: { px: 0.6, fontSize: 13, direction: “rtl”, textAlign: “right”, color: “000000DE”, }, }} onChange={(e) => setSumm(+e.currentTarget.value)} /> ); };
export default YourComponent;
у BTC получается 0.001, а нужно, чтобы было еще число в конце 0.0014
- Assistant
import { useState, useEffect } from "react"; import { FormatPriceProps } from "./FormatPrice.props"; import { useIntl, FormattedNumber } from "react-intl";
const FormatPrice = ({ amount, currency_iso_code, dec }: FormatPriceProps) => { const intl = useIntl(); return ( <FormattedNumber value={amount} style={dec || "currency"} currency={currency_iso_code} minimumFractionDigits={0} maximumFractionDigits={Math.abs(amount) > 10 ? 2 : 4} {...intl.formatNumber()} /> ); };
const YourComponent = () => { const [summ, setSumm] = useState("0.001");
useEffect(() => { const formattedPrice = formatPrice((balanceUsdt / coin?.price) * (percent / 100)); setSumm(formattedPrice); }, [priceSymbol, percent]);
const formatPrice = (value: number) => { return value.toLocaleString("en-US", { style: "decimal", maximumFractionDigits: 4 }); };
return ( <TextField type="number" InputProps={{ disableUnderline: true }} variant="standard" value={summ} sx={{ "& .MuiInputBase-input": { px: 0.6, fontSize: 13, direction: "rtl", textAlign: "right", color: "000000DE", }, }} onChange={(e) => setSumm(e.currentTarget.value)} /> ); };
- User
value приходящее в formatPrice может быть таким 44.1435. нужно, если перед точкой одно число, то maximumFractionDigits 3, если это число 0, то 4, в остальных 2
- Assistant
You can modify the formatPrice function to dynamically set the maximumFractionDigits based on the value before the decimal point. Here's an updated version of the code:
javascriptconst formatPrice = (value: number) => { const digitsBeforeDecimal = Math.floor(value).toString().length; let maximumFractionDigits = 2; if (digitsBeforeDecimal === 1) { maximumFractionDigits = 3; } else if (digitsBeforeDecimal === 2 && Math.floor(value) === 0) { maximumFractionDigits = 4; } return value.toLocaleString("en-US", { style: "decimal", maximumFractionDigits: maximumFractionDigits }); };This will set the
maximumFractionDigitsto 3 when there is only one digit before the decimal point, to 4 when the value before the decimal point is 0, and to 2 for all other cases.