mysql5.0.3,从版本5.0.3起将负数存储在mysql表的十进制字段中

在MySQL 5.0.3之后,DECIMAL类型的字段存储负数时不再存储符号字符,而是使用内部的符号位来表示正负。尽管在PHPMyAdmin中显示带负号,但实际存储不包含负号。文档提到的改变可能会影响依赖旧行为的应用。文章讨论了是否应该使用DECIMAL存储负数,以及与FLOAT类型的比较。建议对于需要精确计算的场景,坚持使用DECIMAL。
摘要由CSDN通过智能技术生成

My table has few fields with an amount column of type decimal.

This column will have either a deposited amount (a positive value) or a withdraw amount (a negative value).

I store the positive value as just 120 and the negative value as -50.

I sum the column and got the result as expected.

Mysql version is: 5.1.33-community.

When i checked mysql documentation about decimal i confused with their description.

Before MySQL 5.0.3, if you inserted

+0003.1 into a DECIMAL(5,1) column, it was stored as +0003.1. As of MySQL

5.0.3, it is stored as 3.1. For negative numbers, a literal -

character is no longer stored.

Applications that rely on the older

behavior must be modified to account

for this change. http://dev.mysql.com/doc/refman/5.0/en/precision-math-decimal-changes.html

When i listed the rows in phpmyadmin i could see the negative sign and when i calculated the result was as expected. but the documentation said no sign will appear.

Is it good to store negative number in decimal(haven't i studied in school? confused)?... or do we have to use float.

I learn't that float would complicate calculations and was advised to stick with decimal for certain conditions.

I would like to have suggestions for this.

解决方案

From what I understand, the documentation is saying it won't store a literal "-" character, which means it's probably now doing what the other signed INTEGER fields have always done and it's storing a sign bit to denote negative numbers instead.

You're still seeing a minus sign preceding the number because it's being generated by MySQL as a result of that sign bit.

If you don't understand the sign bit, you can consider how a signed byte can store numbers from -128 to 127, while an unsigned byte can store numbers from 0 to 255. That's because one of the 8 bits in a signed number is being used to store +/- (1 is negative, 0 is positive), while the remaining bits offer numbers up to 2^7 (-128 or 127).

So, for example, if the bits 1111 had a sign bit they would equal -7 (negative+4+2+1), but if they were unsigned they'd equal 15 (8+4+2+1). It's still the same amount of bits being stored.

You may wonder why the negative bound in a signed number can use the 8th bit, while the positive bound is limited to the sum of the 7 bits (1 less than the 8th bit). This is because 10000000 is considered to be both negative and the 8th bit simultaneously, because its representation of -0 otherwise is redundant with 00000000 which represents 0. There's no distinction between negative and positive zero, so a negative most significant bit is always the value of that bit itself (but negative).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值