开发者

trailing zeroes not going into database

开发者 https://www.devze.com 2023-03-27 16:46 出处:网络
So here is my issue, We are trying to put pricing into our开发者_C百科 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....

So here is my issue, We are trying to put pricing into our开发者_C百科 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...)


10 is the same as 10.00 is the same as 10.0

if you want to display with those trailing digits, or as it's more commonly known 2 decimal places, then you will need to write your code to do that. Since you've not said what language you're using I can't really help you with that part.


It doesn't matter how it looks in the database. What matters is how you present data in your app. 10.00 is the same value as 10, so in the data table it will be saved as 10.

You have to format number before output.

0

精彩评论

暂无评论...
验证码 换一张
取 消