Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert decimal number from the accessor to react-table?

I am using react table. On my data, I have fields that have decimal numbers. I need to apply the function of round off on the data of the decimal numbers. I hope you could help me. Thank you

   const columns = useMemo(
        () => [
          {
            Header: 'Rank',
            id: 'index',
            accessor: (function (_row, i) { return i + 1; })
          
        },
        
          {
            Header: "Username",
            accessor: "username"
          },
          {
            Header: "Score",
            accessor: 'score',
            Cell: (row) => {
              return <div>{Math.round(row)}</div>;
            },
          },
          
        ],
        []
      );

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The function passed to Cell takes the first argument as an object with more data. To round the number by passing a funciton to Cell, use:

Cell: (obj) => Math.round(obj.value)

Or pass a function to the accessor:

accessor: (obj) => Math.round(obj.score)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading