Home > Mobile >  Adding 2 decimal places in textinput
Adding 2 decimal places in textinput

Time:12-07

i need help adding 2 decimal place in after adding a value. for example if i add 100000 then it will convert it to 100,000.00 here is my current code.

<TextInput
            placeholder='0'
            keyboardType = 'decimal-pad'
            underlineColorAndroid="transparent"
            style={{
            width:'90%',
            fontSize: 20,
            fontWeight:'400'}}
            onChangeText={TextInputValue => this.onEnterText(TextInputValue)}
/>

onEnterText = (TextInputValue) =>{
     if(TextInputValue.trim() != 0){
      this.setState({TextInputValue : TextInputValue, ErrorStatus : true}) ;
    }else{
        this.setState({TextInputValue : TextInputValue, ErrorStatus : false}) ;
    }
  }

CodePudding user response:

Try using toFixed(2) for variable approach.

For decimals in input you might find this link useful : https://codesandbox.io/s/9zjo1lp86w

CodePudding user response:

Try below code.

<TextInput
        placeholder='0'
        keyboardType = 'decimal-pad'
        underlineColorAndroid="transparent"
        style={{
        width:'90%',
        fontSize: 20,
        fontWeight:'400'}}
        onChangeText={TextInputValue => 
        this.onEnterText(TextInputValue % 1 == 0 ? TextInputValue ".00" : TextInputValue.toFixed(2))}
  />

The above code definitely works. but I don't know what is inside your onEnterText()

  • Related