Jump to content

ermmm... Array Placement... Anyone


shamuraq

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/190606-ermmm-array-placement-anyone/
Share on other sites

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.