Search code examples
numberpicker

how to setValue in Two NumberPickers from float value


I have two numberpickers

NumberPicker np1 = (NumberPicker)container.findViewById(R.id.np1);
NumberPicker np2 = (NumberPicker)container.findViewById(R.id.np2);

Example: enter image description here

This:

String sValue = np1.getValue() + "." + np2.getValue();

textView.settext(sValue);

return for example 60.5

But i can't know how to put float value back

float f = (float)60.5;

in to my two numberPickers. Help please.


Solution

  • If your numbers will only have 1 decimal place then you could do the following:

    float f = 60.5f;
    
    var left = Math.Truncate(f);    
    var right = (f-left) * 10;
    
    np1.setValue(left);
    np2.setValue(right);