Round value JSON

0

In my code, I get a JSON with a value (Example: 6647), and I would like to round it to only 1 decimal place.

In the code, I get the JSOn content by the call:

  render(){
    return(
      <div className="container">
          <div className="row">
                   <div className="col-md-12 col-lg-6 col-xl-3">
                   <div className = "row">
                       <div className = "info-box">
                           <span className = "info-box-icon logo-View">
                           <i className = "fa fa-tasks"> </i> </span>
                           <div className = "info-box-content">
                                 <span className = "info-box-number">{this.state.data.pageViews}</span>
                                 <span className = "info-box-text"> Page Views </span>
                           </div>
                       </div>
                   </div></div>
            </div>

    );
  }

And the class:

 componentDidMount(){
        let URL = 'algumaURL'
           fetch(URL)
           .then(function(response) {
              let data = response.json()
              return data;
           })
           .then((json) => {
              console.log('parsed json', json)
              this.setState({data : json});
           })
           .catch(function(ex) {
              console.log('parsing failed', ex)
           })
  }

JSON:

{"pageViews":13184}

PageViews value is randomized to every page refresh.

    
asked by anonymous 24.11.2017 / 11:27

1 answer

1

Use the JS function .toFixed () :

  The toFixed () method formats a number using fixed-point notation.

     

Syntax: numObj.toFixed ([digits])

Example:

var num = 6.647; 
//O parâmetro da função é o número de casas decimais.
var numeroFormatado = num.toFixed(1);

console.log(numeroFormatado);
/* OBS.:
Se a casa consecutiva a informada no parâmetro
For superior ou igual a 5 o valor será arrendoado para cima.
*/
    
24.11.2017 / 14:20