java - Setting scale to a negative number with BigDecimal -


does ever make sense set scale negative number this?

bigdecimal result;     .     .     . result = result.setscale(-1, roundingmode.half_up) 

what happens if do?

yes, makes sense set scale negative number in situations. means number rounded nearest 10 -scale number, or 10 in case.

bigdecimal bd = new bigdecimal(1094); bd = bd.setscale(-1, roundingmode.half_up); system.out.println(bd); system.out.println(bd.doublevalue()); 

prints

1.09e+3 1090.0 

the 1.09e+3 equivalent double value 1090.0.

this useful when scientific measurement quantity has less significant digits total number of digits necessary represent number normally. precision of such number less size of number.

if earth's distance sun given 92,960,000 miles, , if value 5 significant digits, equivalent to

bigdecimal bd = new bigdecimal(92960000.0); bd = bd.setscale(-3, roundingmode.half_up);// 3 insignificant/imprecise "0" digits system.out.println(bd); 

which prints

9.2960e+7 

the number rounded nearest 1,000.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -