Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of Locale.getDefault() can result in inconsistent formatting or parsing #886

Closed
petebankhead opened this issue Jan 11, 2022 · 0 comments · Fixed by #887
Closed

Use of Locale.getDefault() can result in inconsistent formatting or parsing #886

petebankhead opened this issue Jan 11, 2022 · 0 comments · Fixed by #887
Labels
Milestone

Comments

@petebankhead
Copy link
Member

Describe the bug

The use of Java's Locale class has caused some trouble in QuPath from the beginning.

This is somewhat mitigated by explicitly allowing the user to specify the Locale and then requesting the appropriate one for format or display (the two available categories).

However, depending upon how the Locale is requested it can return different results. For me, the following Groovy script

import java.util.*
println Locale.getDefault()
println Locale.getDefault(Locale.Category.FORMAT)
println Locale.getDefault(Locale.Category.DISPLAY)

prints

INFO: en_GB
INFO: en_US
INFO: en_US

It is curious that Locale.getDefault() returns something different from the others... and different from what QuPath allows to be specified.

@iwbh15 noticed this caused a problem with the QuPath Align extension and traced it back to Locale in GeometryTools, used in conjunction with a NumberFormat.

To Reproduce
The script above hints there is a problem. To see it in practice requires using QuPath on a computer that uses a different Locale and calling a method that relies upon Locale.getDefault() and uses decimals.... and being surprised.

Basically, it's not that easy to reproduce in practice.
But at the risk of messing up QuPath's preferences

import java.util.*
import java.text.*

Locale.setDefault(Locale.GERMANY)
Locale.setDefault(Locale.Category.FORMAT, Locale.US)
println NumberFormat.getInstance(Locale.getDefault()).parse("0,1234.56")
println NumberFormat.getInstance(Locale.getDefault(Locale.Category.FORMAT)).parse("0,1234.56")

prints the following

INFO: 0.1234
INFO: 1234.56

demonstrating the importance of being consistent. If you run this, be sure to restart QuPath afterwards and check the locale is as before.

Expected behavior
QuPath should never use Locale.getDefault() internally - a category should always be provided.

Desktop (please complete the following information):

  • QuPath v0.3.0 for the alignment problem, but possibly earlier versions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant