Skip to content

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

@petebankhead

Description

@petebankhead

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions