SitePoint Sponsor

# Thread: Decimals & leading zeroes

1. ## Decimals & leading zeroes

Hey,

I got some numbers to perform some basic maths on. Little problem with it, is that some numbers are regular ints, while others look like this: "1,234". If I do those maths on the latter type, they'll lose everything after the comma.

Also, they have leading zeroes. How can I keep those as well?

Thanks in advance,

KonTiki

2. You could str_replace() the commas away.

numbers don't really have leading zeros. So you would need to be pretty specific about what should happen to leading zeros when arithmetic is performed on a number that was extracted from a string with leading zeros. For example:

00001 / 5 = ???
00001 * 1000000 = ???
01 + 0002 = ???

3. Uhmmm, I need to keep the numbers behind the comma somehow. They won't change.

4. Weird things happen with numbers preceded by zeros if you are not expecting it.

PHP Code:
``` \$vals[] = "1,234" ;\$vals[] = "01234" ;\$vals[] = 01234 ;var_dump( \$vals ) ;/* gives:array  0 => string '1,234' (length=5)  1 => string '01234' (length=5)  2 => int 668*/foreach( \$vals as \$val ){ \$val = ltrim( \$val , 0 ); settype(\$val, 'string'); \$newvals[] = str_replace( "," , "" , \$val );}var_dump( \$newvals );/* number 01234 still understood to be octarray  0 => string '1234' (length=4)  1 => string '1234' (length=4)  2 => string '668' (length=3)*/  ```
The more specific and varied the test data you can submit (as in \$vals), the more likely you are to find a solution.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•