Monday, 15 July 2013

java - NumberFormat : setMaximumFractionDigits - not giving expected results -



java - NumberFormat : setMaximumFractionDigits - not giving expected results -

trying figure out why next code not outputting expecting results. please advise. give thanks you.

import java.text.*; public class test { public static void main(string[] args) { string s = "987.123456"; double d = 987.123456d; numberformat nf = numberformat.getinstance(); nf.setmaximumfractiondigits(5); system.out.println(nf.format(d) + " "); seek { system.out.println(nf.parse(s)); } grab (exception e) { system.out.println("got exc"); } } }

output:

987.12346 // expected 987.12345 not 987.12346 987.123456

your sec print doesn't format double you've parsed.

// system.out.println(nf.parse(s)); system.out.println(nf.format(nf.parse(s))); // <-- 987.12346

to output asked for, can add together phone call numberformat#setroundingmode(roundingmode) - like

nf.setmaximumfractiondigits(5); nf.setroundingmode(roundingmode.down);

java

No comments:

Post a Comment