Java对金额算法的BigDecimal类型

在Java中,使用float可以表示浮点数这是个常识,但是float的精度是有问题的,如果涉及金额,那么一般来说就不能用float类型,而是使用BigDecimal,这是个需要掌握的准则。

[b]Commodities,java(商品类)[/b]
public class Commodities{

//商品ID
private int commoditiesid;
//商品名称
private String commoditiesname;
//商品类型
private String commoditiestype;
public int getCommoditiesid() {
return commoditiesid;
}
public void setCommoditiesid(int commoditiesid) {
this.commoditiesid = commoditiesid;
}
public String getCommoditiesname() {
return commoditiesname;
}
public void setCommoditiesname(String commoditiesname) {
this.commoditiesname = commoditiesname;
}
public String getCommoditiestype() {
return commoditiestype;
}
public void setCommoditiestype(String commoditiestype) {
this.commoditiestype = commoditiestype;
}
}
[b]
Commoditiesorder.java(订单类)[/b]
public class Commoditiesorder{
//订单ID
private int orderid;
//商品ID
private int commoditiesid;
//商品数量
private int volume;
//商品金额
private float amount;
public int getOrderid() {
return orderid;
}
public void setOrderid(int orderid) {
this.orderid = orderid;
}
public int getCommoditiesid() {
return commoditiesid;
}
public void setCommoditiesid(int commoditiesid) {
this.commoditiesid = commoditiesid;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
public float getAmount() {
return amount;
}
public void setAmount(float amount) {
this.amount = amount;
}
}

以上是两个实体entity,从表示层(JSP)到模型层(Action)的数据传递通过CommoditiesorderAction来实现
[b]CommoditiesorderAction.java类[/b]
public class CommoditiesorderAction extends ActionSupport{
//订单ID
private int orderid;
//商品ID
private int commoditiesid;
//商品数量
private int volume;
//商品金额
private float amount;
//Commoditiesorder实例
private Commoditiesorder commoditiesorder

public int getOrderid() {
return orderid;
}
public void setOrderid(int orderid) {
this.orderid = orderid;
}
public int getCommoditiesid() {
return commoditiesid;
}
public void setCommoditiesid(int commoditiesid) {
this.commoditiesid = commoditiesid;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
public float getAmount() {
return amount;
}
public void setAmount(float amount) {
this.amount = amount;
}
public Commoditiesorder getCommoditiesorder() {
return commoditiesorder;
}
public void setCommoditiesorder(Commoditiesorder commoditiesorder) {
this.commoditiesorder = commoditiesorder;
}
public String execute()throws Exception{
this.commoditiesorder.setOrderid(this.getOrderid());
this.commoditiesorder.setVolume(this.getVolume());
this.commoditiesorder.setAmount(this.getAmount());
return "success";
}
}
在CommoditiesorderAction.java类中,
[b]this.commoditiesorder.setAmount(this.getAmount())[/b]
amount使用了float类型,但是由于amount的含义是金额,那么就设涉及到主体业务中金额的算法,因此需要对amount进行类型转换。

[b]解决方案:[/b]将Action中的amount类型改为String,在与Commoditiesorder.java交换值时进行处理
//商品金额
private String amount;
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
然后在Action中处理String到BigDecimal类型的转换

//空值处理
if(this.getAmount().equals("")){
this.commoditiesorder.setAmount(null);
}else{
this.commoditiesorder.setAmount(new BigDecimal(this.getAmount()));
}
需要注意的是BigDecimal不支持空值实例化,故要判断空值。另外可以利用Struts提供的
将“空”值转换为null的小技巧,只需在web.xml中设置
<init-param>
<param-name>convertNull</param-name>
<param-value>convertNull</param-value>
</init-param>
这样就不需要对空值进行判断了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值