Universal Robots Forum

Good practice: Reading floats from number input field

When reading a floating point number from an InputTextField, it should be noted, that there can be different decimal notations, based on the users selection of language.
English language would use dot-notation (e.g. 1.5) where e.g. French would use comma-notation (e.g. 1,5) - this is also the raw text that would be read from the input field using the getText() method.
As URScript will need dot-notation for decimal separation, it is good practice to ensure that the number parsed will have this format.

This can e.g. be done in the following ways, depending on whether the input is desired as float or String.

float myFloatVal =  NumberFormat.getInstance().parse(textInField).floatValue();
String textWithNumFormat = NumberFormat.getInstance().parse(textInField).toString();
1 Like

Hi, I have a issue related with this topic. When I’m trying to read a float number with an InputTextField, just like in the Whip URCap sample, the simulator notifies me of an error. It can’t read numbers with decimal notations…

public void onNumanalogChange(InputEvent event){
	if(event.getEventType() == InputEvent.EventType.ON_CHANGE){

		Numero = Float.parseFloat(numanalog.getText());	
		// Se pone la variable analógica al valor leído

How can I read a float number from an InputTextField?
“Numero” is a variable where I save the number read.

Sorry, I just realize how can I fix this. Thank you

I tried to make a function to just run it all through, just to be safe, but it isnt doing what I expect.

	private double returnSafeDouble(String input)
		double output = 0;
			output = NumberFormat.getInstance().parse(input).doubleValue();
		catch (ParseException e)
		System.out.println("Converted " + input + " into " + Double.toString(output));
		return output;

When I feed it 4.5, it returns 4.5, but when I feed it 4,5 it returns 45,0