客户余额规模作为其货币的指数
我在 java hibernate 项目
货币类下有 2 个类,其中包含变量(指数)和 (BigDecimal) DataType
&
具有变量(余额)和(BigDecimal)数据类型的客户类
我想将余额规模设置为指数
示例:
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
....
....
....
我该怎么做?
我的意思是我想将客户余额规模作为其货币的指数
注意: 我尝试 BigDecimal.setScale() 但此方法需要常量整数字段 dataType,其中指数为 BigDecimal 且可变
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
BigDecimal#setScale(...)
应该可以工作:这会产生
rounded = 230.13
。BigDecimal#setScale(...)
should work:This yields
rounded = 230.13
.您应该从 exponent-BigDecimal 中提取整数值并将其用作 setScale 的参数。请注意,您必须设置 RoundingMode:
You should extract the integer-value from the exponent-BigDecimal and use that as parameter to setScale. Be aware, that you have to set a RoundingMode:
您可以在
BigDecimal
上调用intValue()
来获得所需的结果。代码片段:
You can call
intValue()
onBigDecimal
to get the desired result.Code snippet:
不,它不需要常量整数。它可以接受任何整数。您可以使用 BigDecimal.intValue() 来获取所需数据类型的指数。
但是 BigDecimal 对于指数值来说是一个非常奇怪的数据类型选择。您不能认真期望它超出整数范围。
No, it doesn't require a constant integer. It can accept any integer. You can use BigDecimal.intValue() to get the exponent as the datatype required.
But BigDecimal is a very strange choice of datatype for the exponent value. You can't seriously be expecting it to get beyond the range of an integer.