Jump to content

Android and entering floating point numbers


Sensei

Recommended Posts

Hello!

 

Android and entering floating point numbers. With inputType=numericDecimal

 

Has anybody found any sensible solution to this madness.. ?

 

Google has not done anything since at least 2009 AFAIK.

 

This for instance

http://stackoverflow.com/questions/12780125/soft-keyboard-with-comma-instead-of-dot-for-decimal-separator-in-android

doesn't work on my Samsung Galaxy S5..

 

Tried

input.setKeyListener(DigitsKeyListener.getInstance("0123456789.,"));

and the only thing it did, changed "." (dot) key on keyboard,

to ".-,+", but it's just visual (!),

not having any impact on entered characters in text view field..

(still not able to enter comma character on the phone).

 

The only thing that really works is ignoring "numericDecimal" and going with "text" inputType...

 

Best Regards!

 

ps. If you're UK/USA citizen you will probably have no idea about what I am talking about...

But as you can see on the map (middle of page)

https://en.wikipedia.org/wiki/Decimal_mark

https://en.wikipedia.org/wiki/File:DecimalSeparator.svg

It's affecting at least half of human kind..

 

Link to comment
Share on other sites

Maybe I should explain a bit to UK/USA members what is happening (although I doubt you can help without even being able to replicate issue):

If we have textView control, with inputType=numeric or inputType=numericDecimal etc,

there is showed special smaller keyboard with just digits,

and dot keys,

so we can enter f.e. 3.141265,

but half of world is using comma as floating point separator, not dot.

 

When using

Double Double.parseDouble( String string );

dot is treated as separator (even if locale is using comma).

(there is exception while trying to parse string with comma, or it's cut in half)

 

When using

String.format( "%f", double );

where is used locale,

to print double.

 

So, if we print double to text, with comma as separator,

and then try to parse it, with dot as separator,

it will obviously screw up float/double.

 

If we try to use

 

 

String string = textView.getText().toString();
try
{
   NumberFormat format = NumberFormat.getInstance();
   Number number = format.parse( string );
   value = number.doubleValue();
}
catch ( NumberFormatException e ) {
}
catch ( ParseException e ) {
}
parsing and printing will be fine, using comma (locale),

but (virtual) keyboard on smartphone (at least mine) is showing digits keys and dot key, and not comma key,

so not being able to enter correct separator character,

and not being able to enter fraction..

Edited by Sensei
Link to comment
Share on other sites

Here in N.Z. I choose UK English as my default. That means I get keyboards with pound signs, not dollar signs.

 

 

 

(I'm a programmer, but all in the Microsoft world and on PC's and servers - never touched Android in that sense. However, I have a friend who does Android for a living and will see if he's got a comment. Except I'm out of town for two weeks so there'll be a delay.)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.