BigDecimal、精度和小数位数 [英] BigDecimal, precision and scale

查看:87
本文介绍了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)

谢谢!

推荐答案

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 .

精度:

精度是未缩放值中的位数.例如,对于数字 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//比例 = 5,精度 = 5
  • 12340/100000 = 0.1234//比例 = 5,精度 = 4
  • 1/100000 = 0.00001//比例 = 5,精度 = 1

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

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

规模:

如果为零或正数,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

BigDecimal.toString:

BigDecimaltoString 方法根据比例和 precision 表现不同.(感谢@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 scale -1 产生5E+1")
  • 如果 scale >= 0precision - scale -1 >= -6 产生一个纯十进制数(例如 10000000 scale 1 产生1000000.0")
  • 否则,使用 E 符号,例如10 scale 8 产生1.0E-7",因为 precision - scale -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天全站免登陆