尾随零不进入数据库 [英] trailing zeroes not going into database

查看:109
本文介绍了尾随零不进入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里是我的问题,我们试图把定价到我们的数据库,但由于某种原因,它转换10.00到10或10.50到10.5它保持它只是剔除尾随零。....



我检查了SQL语句,它说价格= 10.00 ....所以我真的很困惑为什么/它在剥离.00



Ive检查所有的方式,直到我们输入的数字到最后一步在INSERT或UPDATE,它保持结尾零所有直到UPDATE或INSERT,然后当你检查表...零 c c> c> ,或类似的,用于存储数据?如果不是,那么这是正常的行为。请参阅 http://dev.mysql.com/doc/refman/ 5.0 / en / numeric-types.html



请不要在另一个答案中遵循建议,并将此留给您的表示层。正确地执行作业并存储确切的十进制值。这种方式避免舍入问题。



ps还有一个单独的问题,即如何管理价值使用任何语言管理。希望它使用包括适当格式的特殊类型。如果没有,那么你可能需要担心你如何处理你的代码中的值(一种可能是乘以100并使用整数分,但是你需要注意数学正确地循环 - 这是复杂的,但它是复杂的一个原因;如果你只是东西一切都在双打它可能会很简单,但有一些奇怪的bug某些值...)


So here is my issue, We are trying to put pricing into our database but for some reason it converts 10.00 to 10 OR 10.50 to 10.5 it keeps it just chops off the trailing zeroes....

I checked the SQL statement and it says price=10.00 .... so Im really confused as to why/where it is stripping the .00

Ive checking all the way through where we enter the number to the final step at the INSERT or UPDATE and it keeps the trailing zeroes all the until the UPDATE or INSERT and then when you check the table ...the zeroes are dropped

解决方案

are you using a decimal(N,2) type, or similar, for storing the data? if not, then this is normal behaviour. see http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

please, don't follow the advice in another answer and leave this to your presentation layer. do the job properly and store the exact decimal value. that way you avoid rounding issues.

ps there's also the separate issue of how the value is managed in whatever language you are using. hopefully it uses a special type that includes appropriate formatting. if not, then you may want to worry about how you handle the value in your code (one possibility is to multiple by 100 and use an integer number of cents, but then you need to take care that maths rounds correctly - this is complicated, but it's complicated for a reason; if you just stuff everything in doubles it will likely be simple, but have some strange bug for certain values...)

这篇关于尾随零不进入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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