tags:

views:

185

answers:

7

I have an array of numbers from 5 to 6.4 but the numbers are relating to feet and inches and I am using them for calculation.

When I come to use 5.10 it removes the zero and I have the same output as though it was 5.1.

Is there anyway to prevent PHP removing the 0. I presume it does some sort of default casting.

Thanks in advance

A: 

You could cast it manually using printf() or sprintf()

(Links coming shortly)

Chacha102
A: 

Yea, it's Settype

settype($bar, "string");  // $bar is now "1" (string)

Wait. How is the 0 in 5.10 a leading zero?

I fail at reading comp. printf/sprintf would work better in a case where you are forcing 2 significant figures, regardless of whether the trailing one is a zero.

Satanicpuppy
A: 

You can try using printf

$a = 5.10;

echo $a;            // prints 5.1

printf("%.2f",$a);  // prints 5.10
codaddict
i can't format them after it has been removed because that would been 5.1 would also be 5.10.as stated below i am trying to represent 5 feet and 10 inches
Ben
+9  A: 

If you mean "5 feet and 10 inches", you can't represent that with an integer (or float) - as you see, you'll run into major problems this way.

The computer is doing exactly what you asked from it (not necessarily what you intended). E.g. Google tells me that 5.8 feet == 69.6 inches, which is slightly more than 5 feet 8 inches (== 68 inches). However, (float) 5.10 === (float) 5.1 === (float) 5.100000 (five plus one tenth).

For the US measuring system, you will need something more complicated than an int - as feet and inches are not cleanly convertible to each other in decimal (IIRC, 1 foot = 12 inches, therefore 5 feet 1 inch = 5.083 feet, plus rounding error).

Check out e.g. Zend_Measure; it's a collection of PHP classes which allows you to work with different measuring standards, I think there's support for the US/Imperial system too.

EDIT: if everything else fails, you could convert both numbers to inches - e.g. 5'10" == (5*12) + 10 == 70 inches, which is a nice integer you can work with.

Piskvor
yes that is what i mean. any suggestions on a good way to do this?i have an array of heights, 5.1, 5.2 etc and when i loop through i need to use them for a calculation.
Ben
aside from the zend_measure that piskvor mentioned, an associative array with feet and inches separate is your next best option. such as, array( feet => 5, inches => 1; ); You'll have to overload or replace addition, and other operations though with your own routines --which you'd have to anyway with a float representation.
nlucaroni
Zend Measure is the way to go for me I think.Thanks
Ben
A: 

These values are converted to floats implicitely somewhere in your code. You need to find that line and rewrite it to keep the string type. Do you use these values in mathematical expressions, for example?

soulmerge
+5  A: 

When using non-decimal units such as feet and inches or time, I like to use the smallest unit I'm interested in (seconds or inches) to track values internally and then convert to feet and inches for output purposes. I think you'll find this easier than to try to adapt decimal numbers for this purpose.

jkndrkn
+3  A: 

convert to metric

zaphod
Humorous? Yes, in a way. Preferable in a big picture sense? Yes. Always practical? No.
GreenMatt
While metric is easier to work with (as all the units are decimal), converting back and forth between metric/imperial can quickly turn your code into a mess or even crash your spacecraft ( see http://en.wikipedia.org/wiki/Mars_Climate_Orbiter#The_metric.2Fimperial_mix-up )
Piskvor
I am using metric as well. I am trying to display both...
Ben