简体   繁体   中英

Best column type in mysql for decimal time

I have a timesheet application where people put eg. 3.5 hours worked in a day.

Sometimes someone might put 1.25 I guess.

I stored as float but am now having issues when I retrieve data... Should I have used decimal to 2 or 3 points?.

There are a few ways to tackle this.

If you want to have uniformity in the entries, I'd suggest using a "Fixed-Point Type" value (see more info here ). This would mean that 3.5 hours should always be saved as 3.50.

The other option is to convert all your values to integer values (see more info here ), and in your application layer, convert the values back to "readable" format. For example, you would store 1.25 hours as 125, and in your application layer, divide by 100, therefore getting 1.25. This method is more useful for currency management/calculation applications where a specific level of precision is necessary.

Floats allow for floating decimal point precision, so there is no restriction on uniformity of precision for decimal values...

For your purposes, I'd recommend fixed-point type unless there's complexity that you didn't mention.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM