Reputation: 5056
i am working on a app where the user can store a temperature. he 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 :)
Upvotes: 0
Views: 543
Reputation: 364
$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);
Upvotes: 1
Reputation: 5391
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
Upvotes: 1