JS - Decimal and comma for price

0 votes
43 views
added Mar 29 in Javascript by lcjr Lieutenant (12,240 points)
const formattedPrice = (parseFloat(list.price) % 1 === 0 
    ? parseInt(list.price) 
    : parseFloat(list.price).toFixed(2)
).toLocaleString("en-US");

 

 Example Outputs

Input (list.price)Output (formattedPrice)189999189,999188999.50188,999.5040.004040.5040.50

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...