android decimalformat for arabic, symbol is on right side of number

有些话、适合烂在心里 提交于 2019-12-06 03:48:15

问题


I'm trying to support RTL to left languages, and I'm testing with Arabic (which I know nothing about).

Is the negative/positive symbol supposed to be on the right or left of the number? I think it's supposed to be on the left, but when I use Android's DecimalFormat to put the number in the locale the device is set to, the symbol appears on the right..

Has anyone encountered this, know how to work around it? Best I can think is to print it with parentheses if it's negative, that should get around this but isnt ideal.

EDIT

Sorry, code always helps. If I print the below (and the longitude is negative):

DecimalFormat coord_df = new DecimalFormat("#.000000");
coord_df.format(loc.getLongitude())

It's printed like this

##,######-

where the # signs are numbers in arabic (like those shown on the eastern arabic row here: http://en.wikipedia.org/wiki/Eastern_Arabic_numerals)

I need to get the negative on the correct side of the number (which is the left I believe)

SOLUTION

I ended up just checking whether the device was in RTL whenever I needed to mess with displaying numbers like the answer here: android determine if device is in right to left language/layout

If the device is RTL then I use a negative subpattern with a negative sign as a suffix like this:

"#.000000,#.000000-"

回答1:


Just sharing another approach, in your text views you can set the text direction to Left to Right, it will shows the negative sign correctly.

textView.setTextDirection(TextView.TEXT_DIRECTION_LTR);



回答2:


You can handle this by a specific pattern set in string.xml, separate the symbol (minus) from the absolute value of your number:

In string.xml (standard value)

<string name="number_pattern">%1$s%2$.0f</string>

In string.xml (Arabic value)

<string name="number_pattern">%2$.0f%1$s</string>

In your java program, pass the value parameters (absolute value and symbol) like bellow:

double numberAbs = (number< 0 ? -number: number);
String symbol = (number < 0 ? "-" : "");
return String.format(new Locale("en", "US"), getContext().getString(R.string.number_pattern), symbol, numberAbs);



回答3:


I dont know what is your code but I get this with the code -1,111,11:

        DecimalFormat df2 = new DecimalFormat( "#,###,###.###" );
        Double dd=-1111.11;
        String res=df2.format(dd);
        ((EditText)findViewById(R.id.editText1)).setText(res);


来源:https://stackoverflow.com/questions/26551572/android-decimalformat-for-arabic-symbol-is-on-right-side-of-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!