BigDecimal与Long转换

新建了一个class类 取名叫Firut

import java.math.BigDecimal;

public class Firut {
	private String id;
	private BigDecimal price;
	private long much;
	private int choose;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public long getMuch() {
		return much;
	}

	public void setMuch(long much) {
		this.much = much;
	}

	public int getChoose() {
		return choose;
	}

	public void setChoose(int choose) {
		this.choose = choose;
	}

	public static void main(String[] args) {
		Firut firut = new Firut();
		long price = 1000;
		BigDecimal b = new BigDecimal(100);
		firut.setId("1");
		//long转换成BigDecimal
		firut.setPrice(new BigDecimal(price));
		//BigDecimal转换成long
		firut.setMuch(b.longValue());
		//BigDecimal转换成int
		firut.setChoose(b.intValue());
		System.out.println(firut.getPrice());
	}
}

注意转换的精度问题

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Scroll Up