Number formatting (thousands separator)

Hello,
another small question for my puzzle. :wink:

Is there a way how to do thousand separator different than “,” ??

I can do this easily in .items

Number      PVE_Daily           "Today's Yield [%,.0f Wh]"`

Which would give me instead of 22478 Wh 22,478 Wh but that’s US formatting and I would like to have it like 22 478 Wh

This is not working (do i need to somehow escape " " ?

Number      PVE_Daily           "Today's Yield [% .0f Wh]"`

And I’ve tried as well this in rules

String result = String.format("%,.2f", value).replace(",", " ");

which log rejected as "This expression is not allowed in this context, since it doesn’t cause any side effects."

Thanks for tips! :slight_smile:
(even better would be to have an ability to format all numbers automatically by locale - which is set in configuration)

https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html has the full docs for what is possible for formatting the label.

yes I know that, but as per documentation

‘,’ - - y2 y5 - The result will include locale-specific grouping separators

this is not the case in OH2 because it’s not using locale specific, but only US

Have you set your locale in OH? OH doesn’t actually change any of this. It uses that String formatter as is. There isn’t any way that it could override the documented behavior.

yep I configured locale in paperUI but it seems formatter just ignore this and uses US default

What about the local on the operating system?

Given that a super majority of openHAB developers are European and the majority of openHAB users are European I find it unlikely they would hard code a US default anywhere in the software. There has to be some setting somewhere.

locale is en_CA which should have " " as a separator
I’ll try 2.5 snapshot if it will be working correctly

But who’s locale? openHAB has one, but java has one too, which I imagine gets picked from host OS.

On my (windows) karaf console, the lengthy output from system:property includes
user.country=GB
user.language=en

This chart gives , comma as separator for en_CA (Canada)
https://lh.2xlibre.net/locale/en_CA/
fr_CA on the other hand has space

According to this and this, the thousands separator is actually a comma for en_CA…

hmm i was looking here
https://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html

and separator there looks like space … so question is if java from oracle actually using their definition or whatever.

so so i’ve changed locale to more europian, de_DE and separator is still the same

Number      PVE_Daily           "Today's Yield [%,.0f Wh]"

I guess this interpration simply using , as separator no matter which locale. Or can anybody confirm different result?

If you enter
system:property
on the karaf console, what results do you see for
user.country=
user.language=
This is what I’d expect the formatter to use

user.country=DE
user.country.format=CA
user.language=de
user.language.format=en

strangely there is something .format which i have no idea where to change/how?

humm, looks like the culprit. I had not thought of “sub-settings” at work. The .format version will be used by the formatter only. Seems unlikely it got randomly set to CA. Sorry I do not how to alter, or better remove, these settings. Maybe something in ...openHAB/runtime/bin/setenv

thanks to your hint i was able to find where it is driven
so

user.country=DE
user.language=de
are from /etc/default/locale -> LC_MESSAGE

user.country.format=CA
user.language.format=en
are used from /etc/default/locale -> LANG

so yeah, messed up locales, but ironically messed up by openhabian-config tool :slight_smile:

Sneaky. What was fix, just edit the offending lines, remove them, or re-run the tool?

No openhabian here, presumably why I have no user.xxx.format system settings (and formatter will use the base settings)

fix is pretty easy, just manually update locale and because I want english console but EU numbers etc. I did:

  • Edit /etc/locale.gen and make sure that both en_US.UTF-8 and regional eg. de_DE.UTF-8 are uncommented.
  • Run “sudo locale-gen”
  • Edit /etc/default/locale with this:
LANG=de_DE.UTF-8
LC_MESSAGES=en_US.UTF-8
  • Reboot

fixed
and result from console is

user.country=US
user.country.format=DE
user.language=en
user.language.format=de

and btw OH 2.5 snapshot, dunno if that .format thing is in 2.4 as well

1 Like

Reckon that would work with CA-en and CA-fr :wink:

Well spotted. Will implement as well.

well I think this should not depend on system locale but trully only on OH locale, as many of us are using linux machines as en_US preferably, but end-user apps like OH should act as system independed with their own lang settings

just my two cents ;]