Home > OS >  How to convert int to two decimals in ReactJS?
How to convert int to two decimals in ReactJS?

Time:01-25

I have a table showing JSON data. I need to show the value with decimals, how to achieve this? result.price represents a price. Component is a multiplier for the price.

My table:

  <tbody>
    {this.state.data.map((result) => {
      return (
        <tr>
          <td>{result.name}</td>
          <td> {result.price * component} €</td> 
        </tr>
      )
    })}
  </tbody>

CodePudding user response:

I see you're trying to format money. Maybe you can take a look to the package react-currrency-format that will make your life a lot easier with these formatting; you can even add the currency sign of your choice.

For your use case it'd be something like:

import CurrencyFormat from 'react-currency-format';

<tbody>
{this.state.data.map((result) => {
  return (
    <tr>
      <td>{result.name}</td>
      <td><CurrencyFormat value={result.price * component} displayType={'text'} thousandSeparator={true} suffix={'€'} fixedDecimalScale={true} decimalScale={2} /></td> 
    </tr>
  )
})}

CodePudding user response:

if you want to convert the data to decimal use :

parseFloat(myNumber).toFixed(2);
  •  Tags:  
  • Related