trailing zeroes not going into database

前端 未结 3 693
迷失自我
迷失自我 2021-01-22 10:38

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..

相关标签:
3条回答
  • 2021-01-22 11:07

    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.

    0 讨论(0)
  • 2021-01-22 11:08

    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...)

    0 讨论(0)
  • 2021-01-22 11:14

    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 讨论(0)
提交回复
热议问题