Android decimalformat for arabic, character is to the right of the number

I am trying to support RTL in left-handed languages โ€‹โ€‹and I am testing in Arabic (which I know nothing about).

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

Has anyone encountered this, knows how to get around this? Best I can print it with parentheses if negative, which should get around this, but not ideal.

EDIT

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

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

      

It is printed like this

##,######-

      

where # signs are numbers in Arabic (for example, shown in the East Arabic row here: http://en.wikipedia.org/wiki/Eastern_Arabic_numerals )

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

Decision

I was just checking to see if the device was in RTL whenever I needed to mess around with displaying numbers like the answer here: android detect if device is on the right in 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-"

      

+3


source to share


3 answers


Just using a different approach, in your text views you can set the text direction to left-right, it will show the negative sign correctly.



textView.setTextDirection(TextView.TEXT_DIRECTION_LTR);

      

+1


source


I don't know what your code is, but I get this with -1,111,11 code:



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

      

0


source


You can handle this with a specific set of patterns in your string.xml file, separate the (minus) character 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 value parameters (absolute value and character) like below:

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);

      

0


source







All Articles