java - DecimalFormat decimal places -



java - DecimalFormat decimal places -

currently, have bigdecimal values 3 decimal places shown in jtable. when values 2.500 or 1.000, want have 2.5 , 1.0 instead. that's why tried this:

bigdecimal value; bigdecimal value3 = new bigdecimal((string)model.getvalueat(e.getlastrow(), 1)).setscale(3, bigdecimal.round_half_up); bigdecimal value2 = new bigdecimal((string)model.getvalueat(e.getlastrow(), 1)).setscale(2, bigdecimal.round_half_up); if(valor3.equals(value2)) { bigdecimal value1 = new bigdecimal((string)model.getvalueat(e.getlastrow(), 1)).setscale(1, bigdecimal.round_half_up); if(value3.equals(value1)) value = valuer1; else value = value2; } else value = value3;

but doesn't work. looks '2.500'.equals('2.5') false.

i tried give format jtable @ renderer:

class paramrenderer extends defaulttablecellrenderer { private final decimalformat formatter = new decimalformat( "#.000" ); public void setvalue() { formatter.setminimumfractiondigits(1); formatter.setmaximumfractiondigits(3); } }

but makes no difference @ all.

any other idea?

edit: finaly found on net solution:

bigdecimal trim(bigdecimal n) { seek { while (true) { n = n.setscale(n.scale()-1); } } grab (arithmeticexception e) { // not "real" error: no more trailing zeroes -> exit // setscale() tries eliminate non-zero digit -> out of loop // remember exceptions not recommended exiting loops, seems best way... } homecoming n; }

and works great! give thanks much help anyway :)

the first comparing false, because have different scales - value2 has 2 , value3 has 3. if create them same, works you don't need bigdecimal if want display things. decimal format should work, should set maximum fraction digits 1

java formatting

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -