Format number in jsx React component [duplicate]

淺唱寂寞╮ 提交于 2019-12-22 06:47:07

问题


I am writing a jsx file and want to format the display of numbers in a table. Here is the code for the table:

<tr>
  <td>
    {stringVar}
  </td>
  <td>
    {numberVar}
  </td>
</tr>

The numberVar is being printed directly; how can I display that number with C-style string formatting (I need to set precision value, add commas, and a $ character)?


回答1:


You can use any JS expression to format the value. A popular number formatting library is http://numeraljs.com/ but there are many others of course.

As for prefixing it with $, that's just string concatenation:

{"$" + numberVar}

Or, using string interpolation ES6 syntax :

{`$ ${numberVar}`}


来源:https://stackoverflow.com/questions/29320562/format-number-in-jsx-react-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!