Android string formatiing from xml

Posted by mann on Stack Overflow See other posts from Stack Overflow or by mann
Published on 2012-06-19T21:14:14Z Indexed on 2012/06/19 21:16 UTC
Read the original article Hit count: 187

Filed under:

I am parsing xml from server. One of its node contains data like

<distance>16.3432434324354324km</distance>

I am putting it into hashmap like

for (int i = 0; i < nl.getLength(); i++) {
map.put(KEY_DISTANCE, parser.getValue(e, KEY_DISTANCE));

// adding HashList to ArrayList
menuItems.add(map);
}

Everything is nice. But i want it two decimal places for example i want its value should be

 16.34km rather then 16.343234324342342km

I tried with DecimalFormat twoDForm=new DecimalFormat("##.00");
try{ Double StartVTwo=Double.valueOf(KEY_DISTANCE);

            Double resultDouble1     = Double.valueOf(twoDForm.format(StartVTwo));

            Log.e("check", String.valueOf(resultDouble1));

            }catch (NumberFormatException e){

                Log.e("error"," This is error  ");
            }

But it shows exception and prints this message.

Any help would be appreciated!!

© Stack Overflow or respective owner

Related posts about android