I use axios for API queries and NumberFromat to change the decimal number. Here is the App.js:
import React, { Component } from 'react';
import './App.css';
import axios from 'axios';
import web3 from "./web3";
import NumberFormat from 'react-number-format';
export default class PersonList extends React.Component {
state = {
balance: '',
bn: '',
persons: []
};
async componentDidMount() {
const balance = await web3.eth.getBalance("0x2f6aA9e80385316eEe006AB8bB7943abF333A063") / 1e18;
const bn = financial(balance);
const axios = require('axios');
function financial(x) {
return Number.parseFloat(x).toFixed(2);
};
axios.get(`https://api.coinmarketcap.com/v1/ticker/ethereum/?convert=USD`)
.then(res => {
const persons = res.data;
this.setState({ persons });
})
this.setState({ balance, bn });
};
render() {
return (
<div>
<p>
{this.state.persons.map(person => <p>"portfolioValue": {this.state.bn}, "usdeth": {person.price_usd}</p>)}
</p>
</div>
);
}
}
It's displayed as
"portfolioValue": 0.06, "usdeth": 93.270616772
So, I need to change the number of decimals of the second value like so 93.27. Looks like the easy task, but I stuck with it.
import NumberFormat from 'react-number-format';. Check this out.