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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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