Search code examples
doublecodenameone

Codename One Double with 2 Digits


I am having a double value with a constantly varying number of digits, like :

double d = 0.6645566; 
double d = 0.664555666766; 
double d = 0.66455887656655566; 

I want this double to never have more than two digits, like:

double d = 0.66;

None of the solutions here seems to work, due to limitations in the CN1 APIs, I suppose.

Many thanks in advance for any kind answer. How would I achieve this in Codename One?


Solution

  • You can use:

    String twoDigits = L10NManager.getInstance().format(d, 2);
    

    If you still want to keep the decimal you can do:

    long val = (long)(d * 100);
    d = ((double)val) / 100.0;