shamuraq Posted February 2, 2010 Share Posted February 2, 2010 hello all.... i can't figure out if my code below is syntax error or logical error... pls help... <?php //Converting cents to dollars.cents $a = mt_rand(111,9999); $b = $a/100; $c = str_split($b); if(count($c) == 4 && $c[3] == NULL){ echo "Last digit of three figure is 0".'<br>'; } elseif(count($c) == 5 && $c[4] == NULL){ echo "Last digit of four figure is 0".'<br>'; } echo "$a = $b".'<br>'; ?> it only 'echo' $a = $b but none from this if statement... Thanx in advance... Quote Link to comment Share on other sites More sharing options...
shamuraq Posted February 2, 2010 Author Share Posted February 2, 2010 Anyone? Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 2, 2010 Share Posted February 2, 2010 str_split is not going to return anything for a 0. if the number is for example 4000, you divide by 100 and get 40. There is no zero in place 3 or 4. there will never be a zero returned to the right of the decimal place in a whole integer. why not just take your number and put a dot in the last two digits, if the return is one digit pad it. $n = mt_rand(111,9999); $a=$n/100; $b = sprintf("%01.2f", $a); echo "$b"; HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
shamuraq Posted February 2, 2010 Author Share Posted February 2, 2010 is that regex? Can you explain to me (if you have the time) what "%01.2f" means? Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 2, 2010 Share Posted February 2, 2010 No, not regex. Sprintf and printf are string formatters. %01.2f % - just starts the format 0 - zero, what is used for padding 1 - the minimum before the decimal. so .50 would be 0.50 . - the decimal place 2 - the number of placements that must appear after the decimal f - treat as a floating integer http://php.net/manual/en/function.sprintf.php HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
shamuraq Posted February 3, 2010 Author Share Posted February 3, 2010 Thanx fer the help mate... But just out of curiosity is "if(count($c) == 4 && $c[3] == NULL)" a correct syntax? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.