在本文中,我们将为您详细介绍如何使用django.test.TestCaseassertAlmostEqual()评估两个decimal.Decimal对象的相关知识,此外,我们还会提供一些关于.ne
在本文中,我们将为您详细介绍如何使用 django.test.TestCase assertAlmostEqual() 评估两个 decimal.Decimal 对象的相关知识,此外,我们还会提供一些关于.net – 计算System.Decimal精度和Scale、ASCII 码对照表 | ASCII 编码 | ASCII character codes and html, octal, hex and decimal、BigDecimal bigDecimal = new BigDecimal("44e016912630333") 为啥报错java.lang.NumberFormatException、BigDecima类型数据的处理--Non-terminating decimal expansio的有用信息。
本文目录一览:- 如何使用 django.test.TestCase assertAlmostEqual() 评估两个 decimal.Decimal 对象
- .net – 计算System.Decimal精度和Scale
- ASCII 码对照表 | ASCII 编码 | ASCII character codes and html, octal, hex and decimal
- BigDecimal bigDecimal = new BigDecimal("44e016912630333") 为啥报错java.lang.NumberFormatException
- BigDecima类型数据的处理--Non-terminating decimal expansio
如何使用 django.test.TestCase assertAlmostEqual() 评估两个 decimal.Decimal 对象
如何解决如何使用 django.test.TestCase assertAlmostEqual() 评估两个 decimal.Decimal 对象?
我应该如何在 django 的测试模块中使用 self.assertAlmostEqual()
来处理两个十进制.十进制对象?我运行以下测试并希望它通过测试,但我收到一个错误:
self.assertAlmostEquals(Decimal(1.23456789E-10),Decimal(1.23456789),places=4)
相反,我收到以下错误:
AssertionError: Decimal(''1.23456789E-10'') != Decimal(''1.2345678899999998900938180668163113296031951904296875'') within 4 places (Decimal(''1.234567889876543101093818067'') difference)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
.net – 计算System.Decimal精度和Scale
为了说明,让我们拿一个ToString()表示如下:
d.ToString() = "123.4500"
以下可以说是关于这个十进制的。为了我们的目的,比例被定义为小数点右边的位数。有效比例是相似的,但忽略了在小数部分中出现的任何尾随零。 (换句话说,这些参数被定义为sql小数加上一些额外的参数来解释分数部分尾随零的System.Decimal概念。)
精准度:7
规模:4
> EffectivePrecision:5
> EffectiveScale:2
给定一个任意的System.Decimal,如何有效地计算所有这四个参数,而不转换为String并检查String?该解决方案可能需要Decimal.GetBits。
更多的例子:
Examples Precision Scale EffectivePrecision EffectiveScale 0 1 (?) 0 1 (?) 0 0.0 2 (?) 1 1 (?) 0 12.45 4 2 4 2 12.4500 6 4 4 2 770 3 0 3 0
(?)或者将这些精度解释为零将会很好。
解决方法
这里有一些与你的例子相同的代码。希望你觉得它有用 :)
using System; public class Test { static public void Main(string[] x) { ShowInfo(123.4500m); ShowInfo(0m); ShowInfo(0.0m); ShowInfo(12.45m); ShowInfo(12.4500m); ShowInfo(770m); } static void ShowInfo(decimal dec) { // We want the integer parts as uint // C# doesn't permit int[] to uint[] conversion,// but .NET does. This is somewhat evil... uint[] bits = (uint[])(object)decimal.GetBits(dec); decimal mantissa = (bits[2] * 4294967296m * 4294967296m) + (bits[1] * 4294967296m) + bits[0]; uint scale = (bits[3] >> 16) & 31; // Precision: number of times we can divide // by 10 before we get to 0 uint precision = 0; if (dec != 0m) { for (decimal tmp = mantissa; tmp >= 1; tmp /= 10) { precision++; } } else { // Handle zero differently. It's odd. precision = scale + 1; } uint trailingZeros = 0; for (decimal tmp = mantissa; tmp % 10m == 0 && trailingZeros < scale; tmp /= 10) { trailingZeros++; } Console.WriteLine("Example: {0}",dec); Console.WriteLine("Precision: {0}",precision); Console.WriteLine("Scale: {0}",scale); Console.WriteLine("EffectivePrecision: {0}",precision - trailingZeros); Console.WriteLine("EffectiveScale: {0}",scale - trailingZeros); Console.WriteLine(); } }
ASCII 码对照表 | ASCII 编码 | ASCII character codes and html, octal, hex and decimal
ASCII 码对照表 | ASCII 编码 | ASCII character codes and html, octal, hex and decimal
以下是 ASCII 码对照表 | ASCII 编码 ,可以按 Ctrl+F
快速查找需要的信息
BigDecimal bigDecimal = new BigDecimal("44e016912630333") 为啥报错java.lang.NumberFormatException
如题:BigDecimal bigDecimal = new BigDecimal("44e016912630333") 为啥报错java.lang.NumberFormatException
44e016912630333 就是个数字啊
BigDecima类型数据的处理--Non-terminating decimal expansio
异常提示:
Non-terminating decimal expansion; no exact representable decimal result
备注:在加减乘时都没有出现问题,但是到除法运算时异常;
一个无限循环小数,没有精确可表示的结果
方法:四舍五入模式,将一个无限循环小数进行四舍五入
解决方案:
一:multiply求百分比
BigDecimal brokerage=financeStatisticDto.getTotalWinLossGold().multiply(new BigDecimal(financeStatisticDto.getSelltement())).dive(new BigDecimal(100) );
二:divde求百分比
BigDecimal winLo***ate=finaceStatisticDto.getTotalWinLossGlod().divide(financeStaticDto.getTotalOrderGold(),2,RoundingMode.CEILING).multiply(new BigDecimal(100));
三:devide的函数定义如下
BigDecimal.divide(BigDecimal divisor, int scale, RoundingMode roundingMode) ;
scale为小数位数;
roundingMode为小数模式,模式如下:
ROUND_CEILING
如果 BigDecimal 是正的,则做 ROUND_UP 操作;如果为负,则做 ROUND_DOWN 操作。
ROUND_DOWN
从不在舍弃(即截断)的小数之前增加数字。
ROUND_FLOOR
如果 BigDecimal 为正,则作 ROUND_UP ;如果为负,则作 ROUND_DOWN 。
ROUND_HALF_DOWN
若舍弃部分> .5,则作 ROUND_UP;否则,作 ROUND_DOWN 。
ROUND_HALF_EVEN
如果舍弃部分左边的数字为奇数,则作 ROUND_HALF_UP ;如果它为偶数,则作 ROUND_HALF_DOWN 。
ROUND_HALF_UP
若舍弃部分>=.5,则作 ROUND_UP ;否则,作 ROUND_DOWN 。
ROUND_UNNECESSARY
该“伪舍入模式”实际是指明所要求的操作必须是精确的,,因此不需要舍入操作。
ROUND_UP
总是在非 0 舍弃小数(即截断)之前增加数字。
BigDecimal num = num1.divide(num2,10,RoundingMode.ROUND_CEILING);
关于如何使用 django.test.TestCase assertAlmostEqual() 评估两个 decimal.Decimal 对象的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于.net – 计算System.Decimal精度和Scale、ASCII 码对照表 | ASCII 编码 | ASCII character codes and html, octal, hex and decimal、BigDecimal bigDecimal = new BigDecimal("44e016912630333") 为啥报错java.lang.NumberFormatException、BigDecima类型数据的处理--Non-terminating decimal expansio的相关知识,请在本站寻找。
本文标签: