0

i have a simple program that calculates a double value and prints it. whenever the value of that double is negative the first character of the outputted string is \u200e, ASCII value 8206. i cannot find anything about this online.

        System.out.println((int)DecimalFormat.getInstance().format(-1).charAt(0));
        System.out.println((int)DecimalFormat.getInstance().format(-1.0).charAt(0));
        System.out.println((int)NumberFormat.getInstance().format(1).charAt(0));
        System.out.println((int)NumberFormat.getInstance().format(0).charAt(0));

it prints as follows:

8206
8206
49
48

so i know that every negative value is given this weird prefix, but why? if i use String.format() it doesn't happen. no found documentation on this anywhere.

Anyone encountered this or knows how to explain?

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • I suspect you’ll get different results if you use `getInstance(Locale.ENGLISH)`. – VGR Jul 22 '20 at 22:31

1 Answers1

0

You format a number into String, then take a first character charAt(0), cast it to int and actually print the ASCII code.

So it's no wonder that the code may produce a result like this:

45  // ASCII code of - in your default locale it's another character for negative values
45
49  // ASCII code of 1
48  // ASCII code of 0

8206 is a Unicode character for left-to-right mark.

It is very likely that this character is added to the NumberFormat / DecimalFormat to properly display values left-to-right when an RTL locale is applied as explained in this answer

Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • The issue is i am manipulating strings and that value (8206) comes out of nowhere. there is also a char value for '-' so that's not the problem. the problem is an extra char that does nothing for the display of the string, rather just disrupts its reading. – user3621556 Jul 24 '20 at 17:44
  • Your issue is not reproducible with default `en_US` locale as well as with any available locale including those with right-to-left orientation. Can you provide any details about your locale settings? – Nowhere Man Jul 24 '20 at 21:10