You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@edgent.apache.org by Christofer Dutz <ch...@c-ware.de> on 2018/02/14 21:09:23 UTC

Problem running some examples with non US locales

Hi,

I’m currently working though some of the examples and encountered a problem:

Code like this:
nextValue = Double.valueOf(df.format(nextValue));

Seems to not work correctly in Germany as we have a “,” as decimal separator and “.” as grouping character.
This results numbers like “10,3” being passed into Double.valueOf which causes exceptions.

If I change it to this:

try {
    nextValue = df.parse(df.format(nextValue)).doubleValue();
} catch (ParseException e) {
    // Ignore ...
}

It seems to do what the original codes intention was.

What would be the cleanest way to solve this problem? I seem to be seeing this pattern quite a lot and the try/catch solution sounds quite annoying with all this try-catch handling.

Chris



Re: Problem running some examples with non US locales

Posted by Dale LaBossiere <dm...@gmail.com>.
Just to close off this thread…  +1 on “Precision.round()” especially that the component already depends on commons-math3.
I know you already delivered the fix :-)
Thanks,
— Dale


> On Feb 14, 2018, at 4:13 PM, Christofer Dutz <ch...@c-ware.de> wrote:
> 
> What would you think about eating some more Apache Dogfood?
> 
> <dependency>
>    <groupId>org.apache.commons</groupId>
>    <artifactId>commons-math3</artifactId>
>    <version>3.6.1</version>
> </dependency>
> 
> Then we could use: 
> 
> Precision.round(PI, 3);
> 
> Chris
> 
> 
> Am 14.02.18, 22:09 schrieb "Christofer Dutz" <ch...@c-ware.de>:
> 
>    Hi,
> 
>    I’m currently working though some of the examples and encountered a problem:
> 
>    Code like this:
>    nextValue = Double.valueOf(df.format(nextValue));
> 
>    Seems to not work correctly in Germany as we have a “,” as decimal separator and “.” as grouping character.
>    This results numbers like “10,3” being passed into Double.valueOf which causes exceptions.
> 
>    If I change it to this:
> 
>    try {
>        nextValue = df.parse(df.format(nextValue)).doubleValue();
>    } catch (ParseException e) {
>        // Ignore ...
>    }
> 
>    It seems to do what the original codes intention was.
> 
>    What would be the cleanest way to solve this problem? I seem to be seeing this pattern quite a lot and the try/catch solution sounds quite annoying with all this try-catch handling.
> 
>    Chris
> 
> 
> 
> 


Re: Problem running some examples with non US locales

Posted by Christofer Dutz <ch...@c-ware.de>.
What would you think about eating some more Apache Dogfood?

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-math3</artifactId>
    <version>3.6.1</version>
</dependency>

Then we could use: 

Precision.round(PI, 3);

Chris


Am 14.02.18, 22:09 schrieb "Christofer Dutz" <ch...@c-ware.de>:

    Hi,
    
    I’m currently working though some of the examples and encountered a problem:
    
    Code like this:
    nextValue = Double.valueOf(df.format(nextValue));
    
    Seems to not work correctly in Germany as we have a “,” as decimal separator and “.” as grouping character.
    This results numbers like “10,3” being passed into Double.valueOf which causes exceptions.
    
    If I change it to this:
    
    try {
        nextValue = df.parse(df.format(nextValue)).doubleValue();
    } catch (ParseException e) {
        // Ignore ...
    }
    
    It seems to do what the original codes intention was.
    
    What would be the cleanest way to solve this problem? I seem to be seeing this pattern quite a lot and the try/catch solution sounds quite annoying with all this try-catch handling.
    
    Chris