BigDecimal,精度和规模 [英] BigDecimal, precision and scale

查看:606
本文介绍了BigDecimal,精度和规模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用BigDecimal作为我的数字,例如,使用JPA。我对精确度和规模这两个术语进行了一些研究,但我不明白它们究竟是什么。

I'm using BigDecimal for my numbers in my application, for example, with JPA. I did a bit of researching about the terms 'precision' and 'scale' but I don't understand what are they exactly.

任何人都可以解释''的含义' BigDecimal值的精度'和'比例'?

Can anyone explain me the meaning of 'precision' and 'scale' for a BigDecimal value?

@Column(precision = 11, scale = 2)

谢谢!

推荐答案

A BigDecimal 由两个值定义:任意精度整数和32位整数 scale 。在的BigDecimal 被定义为

A BigDecimal is defined by two values: an arbitrary precision integer and a 32-bit integer scale. The value of the BigDecimal is defined to be .

精确度:

precision 是未缩放值中的位数。
例如,对于数字123.45,返回的精度为5.

The precision is the number of digits in the unscaled value. For instance, for the number 123.45, the precision returned is 5.

因此, precision 表示任意精度整数的长度。以下是具有相同比例但精度不同的数字的几个示例:

So, precision indicates the length of the arbitrary precision integer. Here are a few examples of numbers with the same scale, but different precision:


  • 12345/100000 = 0.12345 // scale = 5,precision = 5

  • 12340/100000 = 0.1234 // scale = 5,precision = 4

  • 1/100000 = 0.00001 // scale = 5,精度= 1

在数字等于零(即0.000)的特殊情况下,精度始终为1.

In the special case that the number is equal to zero (i.e. 0.000), the precision is always 1.

比例:


如果为零或正数,< a href =https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#scale() =noreferrer> scale 是位数小数点右侧。如果是负数,则将数字的未缩放值乘以10来表示比例的否定。例如,-3的比例表示未缩放的值乘以1000.

If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. For example, a scale of -3 means the unscaled value is multiplied by 1000.

这意味着'BigDecimal的整数值'乘以。

This means that the integer value of the ‘BigDecimal’ is multiplied by .

以下是一些具有相同精度的示例,具有不同的比例:

Here are a few examples of the same precision, with different scales:


  • 12345,规模5 = 0.12345

  • 12345,规模4 = 1.2345

  • ...

  • 12345,规模为0 = 12345

  • 12345,等级-1 = 123450

  • 12345 with scale 5 = 0.12345
  • 12345 with scale 4 = 1.2345
  • 12345 with scale 0 = 12345
  • 12345 with scale -1 = 123450

BigDecimal.toString:

toString 方法> BigDecimal 根据比例和精度的不同而有所不同。 (感谢@RudyVelthuis指出这一点。)

The toString method for a BigDecimal behaves differently based on the scale and precision. (Thanks to @RudyVelthuis for pointing this out.)


  • 如果 scale == 0 ,整数只是按原样打印出来。

  • 如果 scale< 0 ,总是使用E-Notation(例如5比例-1产生5E + 1)

  • 如果 scale> = 0 precision - scale -1> = -6 生成一个纯十进制数(例如10000000 scale 1产生1000000.0)

  • 否则,使用电子符号,例如10比例8产生1.0E-7,因为精度 - 比例-1 等于小于-6。

  • If scale == 0, the integer is just printed out, as-is.
  • If scale < 0, E-Notation is always used (e.g. 5 scale -1 produces "5E+1")
  • If scale >= 0 and precision - scale -1 >= -6 a plain decimal number is produced (e.g. 10000000 scale 1 produces "1000000.0")
  • Otherwise, E-notation is used, e.g. 10 scale 8 produces "1.0E-7" since precision - scale -1 equals is less than -6.

更多示例:


  • 19/100 = 0.19 //整数= 19,比例= 2,精度= 2

  • 1/1000 = 0.0001 //整数= 1,比例= 4,精度= 1

这篇关于BigDecimal,精度和规模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆