简体   繁体   English

MySQL中最佳列类型的十进制时间

[英]Best column type in mysql for decimal time

I have a timesheet application where people put eg. 我有一个时间表应用程序,人们把例如。 3.5 hours worked in a day. 一天工作3.5个小时。

Sometimes someone might put 1.25 I guess. 我猜有时候有人会投入1.25。

I stored as float but am now having issues when I retrieve data... Should I have used decimal to 2 or 3 points?. 我以浮点数存储,但是现在检索数据时出现问题...我应该使用十进制到2还是3点?

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. 这意味着3.5小时应始终保存为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. 例如,您将1.25小时存储为125,然后在应用程序层中除以100,因此得到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. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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