how do you format a number to currency when using React native EXPO?

前端 未结 5 1574
深忆病人
深忆病人 2020-12-16 10:45

how do I take a number like 10000 and have it output as $10,000.00?

I even had a problem with String.format(...) with a

5条回答
  •  抹茶落季
    2020-12-16 11:05

    The fastest way is to use react-number-format, as stated above, but don't forget the renderText prop so that React Native don't throw rendering error.

    Follow this step:

    1. Install. Use this command:
    npm i react-number-format
    
    1. Use it in your React Native app like this:
    import NumberFormat from 'react-number-format';
    
    export function ReactNativeNumberFormat({ value }) {
      return (
         {formattedValue}} // <--- Don't forget this!
        />
      );
    }
    

    It's working good. As you can see, I ended up creating my custom component that accept value as prop so I can use it everywhere I need it. Just like this:

      
        
      
    

    Hope can be useful.

    PS: This is my reference => https://github.com/s-yadav/react-number-format/issues/17#issuecomment-395187806.

提交回复
热议问题