繁体   English   中英

React Native 中的货币格式

[英]Currency formatting in react native

我有一个简单的设置来格式化使用数字 js 输入到TextInput字段中的货币值。 使用onChangeText我正在设置输入的值并以这种方式格式化

handleValue(value){
let amount = numeral(value).format('0,0[.]00')
// set amount 
}

但是由于某种原因,添加的小数会不断删除自己。 有没有办法来解决这个问题?

这对我有用 https://snack.expo.io/9qX_VnhFn小数点放在正确的位置

import React, { useState } from 'react';
import { Text, View, StyleSheet, TextInput } from 'react-native';

import numeral from 'numeral';

export default function App() {
  const [value, setValue] = useState('');
  const styles = StyleSheet.create({
    container: {
      flex: 1,
      justifyContent: 'center',
      backgroundColor: '#ecf0f1',

    },
  });
  
  const handleValue = (value) => {
    let amount = numeral(value).format('0,0[.]00');
    setValue(amount);
  };

  return (
    <View style={styles.container}>
      <TextInput
        onChangeText={(value) => handleValue(value)}
        style={{
          height: 40,
          width: 280,
          backgroundColor: 'white',
          borderWidth: 1,
          borderRadius: 4,
          alignSelf: 'center',
          paddingLeft: 15,
        }}
        value={value}
      />
    </View>
  );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM