如何获取BigDecimal的负数

安卓机器人 2011-11-15 03:14:27
比如有一个BigDecimal的对象decimal,按照正常思维是只要-decimal就可以了,但是java好像不支持这种写法。记得java好像不支持有符号数,求各位大神帮忙解决下这个难题
...全文
19570 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ydb7459022 2011-11-15
  • 打赏
  • 举报
回复
我发现我真的是个菜…………
ydb7459022 2011-11-15
  • 打赏
  • 举报
回复

import java.math.BigDecimal;


public class decimal1{

/**
* @param args
*/
public static void main(String[] args){
//a=a-2*a;
BigDecimal a =new BigDecimal("1.2");
BigDecimal b = new BigDecimal("2");
BigDecimal c =a.multiply(b);
a.subtract(c).doubleValue();
System.out.println( a.subtract(c).doubleValue());

}

}
huntor 2011-11-15
  • 打赏
  • 举报
回复 2
不看文档

public BigDecimal negate()
Returns a BigDecimal whose value is (-this), and whose scale is this.scale().

public BigDecimal negate(MathContext mc)
Returns a BigDecimal whose value is (-this), with rounding according to the context settings.
Parameters: mc - the context to use.
Returns: -this, rounded as necessary.
Throws: ArithmeticException - if the result is inexact but the rounding mode is UNNECESSARY.
huntor 2011-11-15
  • 打赏
  • 举报
回复 1
不要无视 negate 方法
qybao 2011-11-15
  • 打赏
  • 举报
回复
简单数值类型才支持+-*/
BigDecimal是对象类型,要通过相应的方法来计算
for example
BigDecimal decimal = new BigDecimal(123);
decimal = decimal.multiply(new BigDecimal(-1));
System.out.println(decimal);
wby13579 2011-11-15
  • 打赏
  • 举报
回复
new BigDecimal("-"+decimal.toString())
ydb7459022 2011-11-15
  • 打赏
  • 举报
回复
decimal=decimal-2*decimal;这样可以吗?猜测一下,不知道行不行?
安卓机器人 2011-11-15
  • 打赏
  • 举报
回复
自己帮自己顶一个

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧