精华内容
下载资源
问答
  • decimal.js 实现了对任意精度的十进制进行计算的 JavaScript 库。 标签:decimal
  • 浮点数的存储规则不精确,因为0.1 转换为二进制是无限循环的。在内存中规定,多余部分截断。所以在计算浮点型或者精度要求比较的时候,使用... while decimal.Decimal(i).quantize(decimal.Decimal("0.00")) != dec...
    • 浮点数的存储规则不精确,因为0.1 转换为二进制是无限循环的。在内存中规定,多余部分截断。所以在计算浮点型或者精度要求比较的时候,使用Deciaml 模块。 或者转为整型来计算,在把计算结果转回去。
    def float_value():
        i = 1
        while decimal.Decimal(i).quantize(decimal.Decimal("0.00")) != decimal.Decimal(1.5).quantize(decimal.Decimal("0.00")):
            i  += 0.1
            print(i)
    
    float_value()
    

    如果是这样就会死循环:

    def float_value():
        i =1
        while i != 1.5:
            i = i+0.1
            print(i)
    
    • 设置小数点位数
    decimal.Decimal(66.88888).quantize(decimal.Decimal("0.00")) # 小数点后两位
    
    展开全文
  • Chapter 23. Precision MathTable of Contents23.1. Types of Numeric Values... DECIMAL Data Type Changes23.3. Expression Handling23.4. Rounding Behavior23.5. Precision Math ExamplesMySQL 5 introduces p...

    Chapter 23. Precision Math

    Table of Contents

    23.1. Types of Numeric Values

    23.2. DECIMAL Data Type Changes

    23.3. Expression Handling

    23.4. Rounding Behavior

    23.5. Precision Math Examples

    MySQL 5 introduces precision math, that is, numeric value handling that results in more accurate results and more control over invalid values than in earlier versions of MySQL. Precision math is based on two implementation changes:

    The introduction of new SQL modes in MySQL 5.0.2 that control how strict the server is about accepting or rejecting invalid data.

    The introduction in MySQL 5.0.3 of a library for fixed-point arithmetic.

    These changes have several implications for numeric operations:

    More precise calculations.

    For exact-value numbers, calculations do not introduce floating-point error. Instead, exact precision is used. For example, a number such as .0001 is treated as an exact value rather than as an approximate value, and summing it 10,000 times produces a result of 1, not a value "close" to 1.

    Well-defined rounding behavior.

    For the exact-value numbers, the result of ROUND() depends on its argument, not on factors such as how the underlying C library works.

    Improved platform independence.

    Operations on exact numeric values are the same across different platforms such as Windows and Unix.

    Control over invalid value handling.

    Overflow and division by zero are detectable and can be treated as errors. For example, you can treat a value that is too large for a column as an error rather than having the value truncated to lie within the range of the column's data type. Similarly, you can treat division by zero as an error rather than as an operation that produces a result of NULL. The choice of which approach to take is determined by the setting of the sql_mode system variable.

    An important result of these changes is that MySQL provides improved compliance with standard SQL.

    The following discussion covers several aspects of how precision math works (including possible incompatibilities with older applications). At the end, some examples are given that demonstrate how MySQL 5 handles numeric operations more precisely than before.

    Microsoft Windows 2000 [Version 5.00.2195]

    (C) Copyright 1985-2000 Microsoft Corp.

    C:/Documents and Settings/Administrator>d:

    D:/>dir my*

    Volume in drive D is Local Disk

    Volume Serial Number is D492-84BA

    Directory of D:/

    2005-07-26  15:55      

    2005-07-26  21:30      

    2005-04-27  13:52      

    0 File(s)              0 bytes

    3 Dir(s)   2,707,423,232 bytes free

    D:/>

    D:/>cd *win32

    D:/mysql-5.0.9-beta-win32>cd bin

    D:/mysql-5.0.9-beta-win32/bin>mysql -u root

    Welcome to the MySQL monitor.  Commands end with ; or /g.

    Your MySQL connection id is 2 to server version: 5.0.9-beta-nt-max

    Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

    mysql> create table test (n decimal(15,2));

    ERROR 1046 (3D000): No database selected

    mysql> use test

    Database changed

    mysql> create table test (n decimal(15,2));

    Query OK, 0 rows affected (0.05 sec)

    mysql> insert table test values(99999999.99);

    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that

    corresponds to your MySQL server version for the right syntax to use near 'table

    test values(99999999.99)' at line 1

    mysql> insert into test values(99999999.99);

    Query OK, 1 row affected (0.00 sec)

    mysql> insert into test select * from test;

    Query OK, 1 row affected (0.00 sec)

    Records: 1  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 2 rows affected (0.00 sec)

    Records: 2  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 4 rows affected (0.00 sec)

    Records: 4  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 8 rows affected (0.00 sec)

    Records: 8  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 16 rows affected (0.00 sec)

    Records: 16  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 32 rows affected (0.00 sec)

    Records: 32  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 64 rows affected (0.00 sec)

    Records: 64  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 128 rows affected (0.00 sec)

    Records: 128  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 256 rows affected (0.00 sec)

    Records: 256  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 512 rows affected (0.00 sec)

    Records: 512  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 1024 rows affected (0.00 sec)

    Records: 1024  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 2048 rows affected (0.02 sec)

    Records: 2048  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 4096 rows affected (0.00 sec)

    Records: 4096  Duplicates: 0  Warnings: 0

    mysql> select count(*)  from test;

    +----------+

    | count(*) |

    +----------+

    |     8192 |

    +----------+

    1 row in set (0.00 sec)

    mysql> select sum(n) from test;

    +-----------------+

    | sum(n)          |

    +-----------------+

    | 819199999918.08 |

    +-----------------+

    1 row in set (0.00 sec)

    mysql>

    2.10.1. Upgrading from Version 4.1 to 5.0

    SQL Changes:

    DECIMAL columns now are stored in a more efficient format. To convert a table to use the new DECIMAL type, you should do an ALTER TABLE on it. The ALTER TABLE also will change the table's VARCHAR columns to use the new VARCHAR column type. For information about possible incompatibilities with old applications, see Chapter 23, Precision Math.

    MySQL 5.0.3 and up uses precision math when calculating with DECIMAL values (64 decimal digits) and for rounding exact-value numbers. See Chapter 23, Precision Math.

    As of MySQL 5.0.3, trailing spaces no longer are removed from values stored in VARCHAR and VARBINARY columns. The maximum length for VARCHAR or VARBINARY now is 65,535 characters or bytes, respectively.

    Note: If you create a table with new VARCHAR or VARBINARY columns in MySQL 5.0.3 or up, the table will not be usable if you downgrade to a version older than 5.0.3. Dump the table before downgrading and then reload it after downgrading.

    As of MySQL 5.0.3, BIT is a separate data type, not a synonym for TINYINT(1). See Section 11.1.1, “Overview of Numeric Types”.

    SQL Changes:

    DECIMAL columns now are stored in a more efficient format. To convert a table to use the new DECIMAL type, you should do an ALTER TABLE on it. The ALTER TABLE also will change the table's VARCHAR columns to use the new VARCHAR column type. For information about possible incompatibilities with old applications, see Chapter 23, Precision Math.

    MySQL 5.0.3 and up uses precision math when calculating with DECIMAL values (64 decimal digits) and for rounding exact-value numbers. See Chapter 23, Precision Math.

    As of MySQL 5.0.3, trailing spaces no longer are removed from values stored in VARCHAR and VARBINARY columns. The maximum length for VARCHAR or VARBINARY now is 65,535 characters or bytes, respectively.

    Note: If you create a table with new VARCHAR or VARBINARY columns in MySQL 5.0.3 or up, the table will not be usable if you downgrade to a version older than 5.0.3. Dump the table before downgrading and then reload it after downgrading.

    As of MySQL 5.0.3, BIT is a separate data type, not a synonym for TINYINT(1). See Section 11.1.1, “Overview of Numeric Types”.

    展开全文
  • Chapter 23. Precision MathTable of Contents23.1. Types of Numeric Values... DECIMAL Data Type Changes23.3. Expression Handling23.4. Rounding Behavior23.5. Precision Math ExamplesMySQL 5 introduces p...

    Chapter 23. Precision Math

    Table of Contents

    23.1. Types of Numeric Values

    23.2. DECIMAL Data Type Changes

    23.3. Expression Handling

    23.4. Rounding Behavior

    23.5. Precision Math Examples

    MySQL 5 introduces precision math, that is, numeric value handling that results in more accurate results and more control over invalid values than in earlier versions of MySQL. Precision math is based on two implementation changes:

    The introduction of new SQL modes in MySQL 5.0.2 that control how strict the server is about accepting or rejecting invalid data.

    The introduction in MySQL 5.0.3 of a library for fixed-point arithmetic.

    These changes have several implications for numeric operations:

    More precise calculations.

    For exact-value numbers, calculations do not introduce floating-point error. Instead, exact precision is used. For example, a number such as .0001 is treated as an exact value rather than as an approximate value, and summing it 10,000 times produces a result of 1, not a value "close" to 1.

    Well-defined rounding behavior.

    For the exact-value numbers, the result of ROUND() depends on its argument, not on factors such as how the underlying C library works.

    Improved platform independence.

    Operations on exact numeric values are the same across different platforms such as Windows and Unix.

    Control over invalid value handling.

    Overflow and division by zero are detectable and can be treated as errors. For example, you can treat a value that is too large for a column as an error rather than having the value truncated to lie within the range of the column's data type. Similarly, you can treat division by zero as an error rather than as an operation that produces a result of NULL. The choice of which approach to take is determined by the setting of the sql_mode system variable.

    An important result of these changes is that MySQL provides improved compliance with standard SQL.

    The following discussion covers several aspects of how precision math works (including possible incompatibilities with older applications). At the end, some examples are given that demonstrate how MySQL 5 handles numeric operations more precisely than before.

    Microsoft Windows 2000 [Version 5.00.2195]

    (C) Copyright 1985-2000 Microsoft Corp.

    C:/Documents and Settings/Administrator>d:

    D:/>dir my*

    Volume in drive D is Local Disk

    Volume Serial Number is D492-84BA

    Directory of D:/

    2005-07-26  15:55      

    2005-07-26  21:30      

    2005-04-27  13:52      

    0 File(s)              0 bytes

    3 Dir(s)   2,707,423,232 bytes free

    D:/>

    D:/>cd *win32

    D:/mysql-5.0.9-beta-win32>cd bin

    D:/mysql-5.0.9-beta-win32/bin>mysql -u root

    Welcome to the MySQL monitor.  Commands end with ; or /g.

    Your MySQL connection id is 2 to server version: 5.0.9-beta-nt-max

    Type 'help;' or '/h' for help. Type '/c' to clear the buffer.

    mysql> create table test (n decimal(15,2));

    ERROR 1046 (3D000): No database selected

    mysql> use test

    Database changed

    mysql> create table test (n decimal(15,2));

    Query OK, 0 rows affected (0.05 sec)

    mysql> insert table test values(99999999.99);

    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that

    corresponds to your MySQL server version for the right syntax to use near 'table

    test values(99999999.99)' at line 1

    mysql> insert into test values(99999999.99);

    Query OK, 1 row affected (0.00 sec)

    mysql> insert into test select * from test;

    Query OK, 1 row affected (0.00 sec)

    Records: 1  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 2 rows affected (0.00 sec)

    Records: 2  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 4 rows affected (0.00 sec)

    Records: 4  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 8 rows affected (0.00 sec)

    Records: 8  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 16 rows affected (0.00 sec)

    Records: 16  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 32 rows affected (0.00 sec)

    Records: 32  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 64 rows affected (0.00 sec)

    Records: 64  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 128 rows affected (0.00 sec)

    Records: 128  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 256 rows affected (0.00 sec)

    Records: 256  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 512 rows affected (0.00 sec)

    Records: 512  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 1024 rows affected (0.00 sec)

    Records: 1024  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 2048 rows affected (0.02 sec)

    Records: 2048  Duplicates: 0  Warnings: 0

    mysql> insert into test select * from test;

    Query OK, 4096 rows affected (0.00 sec)

    Records: 4096  Duplicates: 0  Warnings: 0

    mysql> select count(*)  from test;

    +----------+

    | count(*) |

    +----------+

    |     8192 |

    +----------+

    1 row in set (0.00 sec)

    mysql> select sum(n) from test;

    +-----------------+

    | sum(n)          |

    +-----------------+

    | 819199999918.08 |

    +-----------------+

    1 row in set (0.00 sec)

    mysql>

    2.10.1. Upgrading from Version 4.1 to 5.0

    SQL Changes:

    DECIMAL columns now are stored in a more efficient format. To convert a table to use the new DECIMAL type, you should do an ALTER TABLE on it. The ALTER TABLE also will change the table's VARCHAR columns to use the new VARCHAR column type. For information about possible incompatibilities with old applications, see Chapter 23, Precision Math.

    MySQL 5.0.3 and up uses precision math when calculating with DECIMAL values (64 decimal digits) and for rounding exact-value numbers. See Chapter 23, Precision Math.

    As of MySQL 5.0.3, trailing spaces no longer are removed from values stored in VARCHAR and VARBINARY columns. The maximum length for VARCHAR or VARBINARY now is 65,535 characters or bytes, respectively.

    Note: If you create a table with new VARCHAR or VARBINARY columns in MySQL 5.0.3 or up, the table will not be usable if you downgrade to a version older than 5.0.3. Dump the table before downgrading and then reload it after downgrading.

    As of MySQL 5.0.3, BIT is a separate data type, not a synonym for TINYINT(1). See Section 11.1.1, “Overview of Numeric Types”.

    SQL Changes:

    DECIMAL columns now are stored in a more efficient format. To convert a table to use the new DECIMAL type, you should do an ALTER TABLE on it. The ALTER TABLE also will change the table's VARCHAR columns to use the new VARCHAR column type. For information about possible incompatibilities with old applications, see Chapter 23, Precision Math.

    MySQL 5.0.3 and up uses precision math when calculating with DECIMAL values (64 decimal digits) and for rounding exact-value numbers. See Chapter 23, Precision Math.

    As of MySQL 5.0.3, trailing spaces no longer are removed from values stored in VARCHAR and VARBINARY columns. The maximum length for VARCHAR or VARBINARY now is 65,535 characters or bytes, respectively.

    Note: If you create a table with new VARCHAR or VARBINARY columns in MySQL 5.0.3 or up, the table will not be usable if you downgrade to a version older than 5.0.3. Dump the table before downgrading and then reload it after downgrading.

    As of MySQL 5.0.3, BIT is a separate data type, not a synonym for TINYINT(1). See Section 11.1.1, “Overview of Numeric Types”.

    展开全文
  • 代码如下: ...#十进制计算 from decimal import Decimal as D from decimal import getcontext print getcontext() print D(1.0)/D(3.0) print type(D(1.0)/D(3.0)) print (D(1.0)/D(3.0))*D(3

    代码如下:

    #encoding=utf-8
    print '中国'
    
    #十进制计算 注意decimal仍然是浮点
    
    from decimal import Decimal as D
    from decimal import getcontext 
    
    print getcontext()
    
    print D(1.0)/D(3.0)
    print type(D(1.0)/D(3.0))
    print (D(1.0)/D(3.0))*D(3.0)
    
    print .3/3
    print (.3/3)*3

    打印结果如下:

    中国
    Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[Overflow, DivisionByZero, InvalidOperation])
    0.3333333333333333333333333333
    <class 'decimal.Decimal'>
    0.9999999999999999999999999999
    0.1
    0.3

    展开全文
  • Chapter 23. Precision MathTable of Contents23.1. Types of Numeric Values 23.2. DECIMAL Data Type Changes 23.3. Expression Handling 23.4. Rounding Behavior 23.5. Precision Math Examples MySQL 5 introdu
  • 展开全部需要用decimal小数模块e5a48de588b662616964757a686964616f31333365633835importdecimaldecimal.getcontext().prec=1000#加大精度1000数位print("{0:f}".format(decimal.Decimal(2000).exp()))'...
  • 展开全部>>>importdecimal>>>decimal.getcontext().prec=2000>...(decimal.Decimal(2000).exp()*decimal.Decimal(100).exp()).to_eng_string()'1043306701073309013703637376030230195089...
  • from decimal import * a =Decimal('0.1')+Decimal('0.1')+Decimal('0.1')+Decimal('0.3') float(a) >>>>>0.6 转载于:https://www.cnblogs.com/shmily3929/p/9103654.html
  • python decimal 精确计算

    2020-09-22 12:51:57
    1.可以传递给Decimal整型或者字符串参数,但不能是浮点数据,因为浮点数据本身就不准确。需要先将浮点数转化为字符串 传入浮点数 5.55 和 传入字符串 ‘5.55’ from decimal import * print(Decimal(5.55)*100) ...
  • Python提供了decimal模块用于十进制数学计算,它具有以下特点: 提供十进制数据类型,并且存储为十进制数序列;有界精度:用于存储数字的位数是固定的,可以通过decimal.getcontext().prec=x 来设定,不同的数字可以...
  • decimal 精确计算模块

    2019-04-03 14:43:27
    decimal 这个模块提供了十进制浮点运算支持 decimal模块中,可以通过整数,字符串构建decimal.Decimal对象。 如果是浮点数,特别注意,因为浮点数本身存在误差,需要先将浮点数转化为字符串。 精度提升的同时,...
  • 该楼层疑似违规已被系统折叠隐藏此楼查看此楼Python提供了decimal模块用于十进制数学计算,它具有以下特点:提供十进制数据类型,并且存储为十进制数序列;有界精度:用于存储数字的位数是固定的,可以通过decimal....
  • Python提供了decimal模块用于十进制数学计算,它具有以下特点: 提供十进制数据类型,并且存储为十进制数序列; 有界精度:用于存储数字的位数是固定的,可以通过decimal.getcontext().prec=x 来设定,不同的数字...
  • [python计算金额]decimal常用操作

    千次阅读 2017-10-31 10:25:03
    decimal模块 简介 decimal意思为十进制,这个模块提供了十进制浮点运算支持。 常用方法 1.可以传递给Decimal整型或者字符串参数,但不能是浮点数据,因为浮点数据本身就不准确。 2.要从浮点数据转换...
  • 浮点型加法 0.1+0.2 Out[15]: 0.30000000000000004 ...decimal.Decimal(0.1) + decimal.Decimal(0.2) Out[17]: Decimal('0.3000000000000000166533453694') # 设置精度 decimal.getcontext().pre...
  • python 中的 decimal 模块提供十... 为了消除这种误差,可以用 decimal 模块进行更加精确的浮点计算。 from decimal import Decimal #参数这里是字符串 a = Decimal('1.1') b = Decimal('2.2') print(a + b) 运算
  • sql server的 money 类型其实就是小数类型 decimal ,我不喜欢用它,因为有一次什么工具生成,发现它自动把money类型转换成了decimal类型了,与其让它转,还不如自己设计数据库时将货币类型字段设置为 decimal 类型...
  • python decimal精确计算 python decimal精确计算 (1). Decimal接收int和string类型参数 from decimal import * # 不能传入浮点数据,因为浮点数据就不准确 a = Decimal(3.33)*100 b = Decimal('3.33')*100 c = ...
  • Decimal

    2015-11-23 22:03:01
    python 中处理浮点数常用方法是float,但是由于计算机保存浮点数...一般情况下这样处理是没有问题,但当我们处理货币等确定精度数字时需要引入十进制数字运算decimal >>from decimal import * >> a = Decimal(str('2.
  • Mongodb 3.4就开始支持Decimal类型,解决double的精度问题,但是不太好用,MapReduce的时候Array.sum也不能计算Decimal.比较坑,但是聚合可以用 SpringMvc @RequestBody对新的类型转换JSON都不太好,例如LocalDate...
  • 直到最近在一个项目中遇到了小数点计算的问题,发现要解决小数计算的精度问题,还真得用到这个Decimal不可。 文章目录问题描述解决办法四舍五入Decimal 问题描述 浮点数在内存中存储的时候因为存储机制的原因,天然...
  • java.lang.ArithmeticException: Non-...计算除法,出错。 解决方法: 把BigDecimal的 .divide(new BigDecimal(orderNum + “”)) 改成 .divide(new BigDecimal(orderNum + “”), 4, BigDecimal.ROUND_UP) ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,960
精华内容 784
关键字:

计算decimal