开发者

how to save celsius / fahrenheit Value

开发者 https://www.devze.com 2023-04-05 12:37 出处:网络
i am working on a app where the user can store a temperature. he开发者_JAVA技巧 can choose between celsius / fahrenheit

i am working on a app where the user can store a temperature. he开发者_JAVA技巧 can choose between celsius / fahrenheit

now my question is, whats the best way to save the value? IN a complete own format or as fahrenheit, rather celsius?

Just because 91.45F & 91.46F are both 33.03C so would not make sense to save in Celsius..

I am not a math prof. and don't know what other probs could appear. So i just like to ask if someone has experience how to go around any possible problem. I just like to save the value in a format so i can be sure it will be always right recalculated what ever format the user wish and entered before.

edit: the User Input is just 2 values after Comma! edit2: till now it looks the best way seems to always convert the user input into fahrenheit and save that with a precision like 5 values after comma. any other idea. otherwise i would soon close that topic :)


Expanding on what Matt Ball said, you need to remember that if you are doing any calculations on the stored number (besides simply retrieving it), it is best to store it as an integer value, rather than a floating point number. So, if you need precision to the hundreds place, store the captured value as something like temp = captured_value * 100


Store the value and units separately.


$f = 91.45400000;

$c = ($f - 32) × (5/9);

$f = ($c × (9/5)) + 32;

You could store the values with more decimals and just display them with fewer.. That way you keep the presision.

echo "Farenheit: " . round($f, 2);

0

精彩评论

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

关注公众号