How to remove deciales in React Native

1

How can I remove decimal in react native ?? this is the calculation

calculateSum = () => {
const { peso, altura } = this.state;

this.setState({
  imc: (Number(peso) / (Number(altura)*Number(altura))) * 10000
});

}

shows me for example 24,8787321837812 and I just want you to give me 2 decimals 24.87 ??? I tried the toFixed method but it did not work out.

    
asked by Edgardo Escobar 01.11.2017 в 22:32
source

1 answer

1

React is a JavaScript library, and in JavaScript there is the standard method toFixed to format decimals. You just have to apply that function in your code when you assign imc and it should work for you:

calculateSum = () => {
  const { peso, altura } = this.state;

  this.setState({
    imc: ((Number(peso) / (Number(altura)*Number(altura))) * 10000).toFixed(2);
  });
}
    
answered by 02.11.2017 / 20:00
source