JScience unit formatting

Originally published November 17, 2008.
Information provided in this post may be out of date.

JScience uses Unicode characters in the String representations of some of its Units. For example, Unicode U+2103 (℃) is used to represent the unit for degrees Celsius. Those characters can cause problems with fonts and/or platforms (Windows, typically) which do not contain or recognize them. Instead, you may see a question mark, box, or other unfamiliar glyph. Here is the symbol for degrees Celsius again – ℃ – can you see it properly?

Using UnitFormat.label(), we can override the String representation to something more compatible, useful, or just different. Here’s how I changed degrees Celsius to be more compatible with my co-workers Windows systems.

UnitFormat.getInstance().label(SI.CELSIUS, "\u00B0" + "C");

And here’s how I changed knots to be represented by kts instead of the default, kn.

UnitFormat.getInstance().label(NonSI.KNOT, "kts");