Friday, 15 May 2015

android - Is language conditional in some operations? -



android - Is language conditional in some operations? -

i'm developing app utilize geocoder place's coordinates.

the operative this:

the user defines address. the geocoder finds address , coordinates address. this coordinates in decimal format , need them in degrees-minutos format them.

to format coordinates decimal degrees-minutes use:

string frmtlatitude = location.convert(double.parsedouble(lat), location.format_minutes);

so, if have illustration latitude 43.249591 in decimal value, returns 43:14.97546.

after this, have create operations latitude appearance: 4314.975

when operations, 1 of them split value using ".". split 14.97546 in 1 hand 14 , in other 97546.

until here, ok. works fine when have phone's language selected in english. if select in spanish, app crashes. have followed stacktrace , points there. in english language when using first commented function convert decimal degrees-minutes separates decimals "." if have in spanish, separates them ",".

can happen or cause thing?

we can @ source code of convert method

public static string convert(double coordinate, int outputtype) { if (coordinate < -180.0 || coordinate > 180.0 || double.isnan(coordinate)) { throw new illegalargumentexception("coordinate=" + coordinate); } if ((outputtype != format_degrees) && (outputtype != format_minutes) && (outputtype != format_seconds)) { throw new illegalargumentexception("outputtype=" + outputtype); } stringbuilder sb = new stringbuilder(); // handle negative values if (coordinate < 0) { sb.append('-'); coordinate = -coordinate; } decimalformat df = new decimalformat("###.#####"); if (outputtype == format_minutes || outputtype == format_seconds) { int degrees = (int) math.floor(coordinate); sb.append(degrees); sb.append(':'); coordinate -= degrees; coordinate *= 60.0; if (outputtype == format_seconds) { int minutes = (int) math.floor(coordinate); sb.append(minutes); sb.append(':'); coordinate -= minutes; coordinate *= 60.0; } } sb.append(df.format(coordinate)); homecoming sb.tostring(); }

we can see uses decimalformat given pattern. so, if decimalformat constructor :

public decimalformat(string pattern) { // applypattern after symbols set this.symbols = new decimalformatsymbols(locale.getdefault()); applypattern(pattern, false); }

we can see here if give pattern, uses locale values. javadoc said :

parameters:

pattern non-localized pattern string.

to finish, can go here see different local variant of numbers representation : http://docs.oracle.com/cd/e19455-01/806-0169/overview-9/index.html

so can see us-english utilize "dot format" , spanish utilize "comma format".

to reply question : proflem you're facing due decimal format of locale. advice careful when converting types of objects create manipulation on them. converting int string should display it.

i think should seperate decimal part of number when stills float (or decimal type) , convert object string display it. can take @ math class or search illustration on how ;)

also, @dmitry said, can decimalseparator decimalformatsymbols.getdecimalseparator().

sources

location.convert(double,int) source code

decimalformat(string) source code

java "decimal , thousands separators"

android

No comments:

Post a Comment