i have 2 classes under java hibernate project
currency class with variable (exponent) with (BigDecimal) DataType
&
customer class with variable (balance) with (BigDecimal) DataType
i want to set scale of balance as exponent
example:
1) if exponent = 2, and balance = 230.1340098 then balance must be 230.13
2) if exponent = 1, and balance = 230.1340098 then balance must be 230.1
3) if exponent = 3, and balance = 230.1340098 then balance must be 230.134
4) if exponent = 0, and balance = 230.1340098 then balance must be 230
....
....
....
how can i do this?
what's i mean i want to put the customer balance scale as the exponent of his currency
Note: i try BigDecimal.setScale() but this method required constant integer field dataType where exponent is BigDecimal and variable
BigDecimal#setScale(...)
should work:
BigDecimal balance = BigDecimal.valueOf( 230.1340098 );
BigDecimal exponent = new BigDecimal( 2 ) ; //randomly chosen
BigDecimal rounded = balance.setScale( exponent.intValue(), RoundingMode.HALF_UP ) ); //you can use another rounding mode
This yields rounded = 230.13
.