Jump to content

Using Digits also for();


MemphiS

Recommended Posts

First question:

 

i wish for my data in the db to be placed in as 0.00..

Currently im doing this:

<?php
$po = 4; // actual figure comes from the database..
$po1 = 100; // actual figure comes from the database..
$rand1 = rand(0,$po);
$rand2 = rand(0,$po1);

$overAll = $rand1+".$rand2";

output: 2.78 // Example...
?>

 

Is there a better way of doing this? im sure there is

 

My other question is:

 

I wish to do the following:

 

Example:

<?php
$u1 = 200; // actual figure comes from the db
$u2 = 1000; // actual figure comes from the db
$main = 10000;

for ($i = 0; $i < $main; $i++){
$u1P = rand(0,$u1);
$main = $main - $u1P;
echo("U1 takes $u1P off main");
$main = $main - $u2P;
$u2P = rand(0,$u2);
echo("U2 takes $u2P off main");
}
?>

Now the problem with this is that it is going always go U1 first then U2.. I want it to be random so U1 could go say 10 times if U1 number is greater then U2s

 

Hope ive explained it enough for someone to understand

 

Thanks for your responces ;)

Link to comment
https://forums.phpfreaks.com/topic/59791-using-digits-also-for/
Share on other sites

You can use the printf()/sprintf() functions to format variables for output to the browser.

 

<?php
$po = 4;
$po1 = 99;//i assumed the max here would be 99 if its supposed to be two digits?
printf('%.2f',rand(0,$po).'.'.rand(0,$po1));//the .2 specifies 2 decimal points
?>

 

As for your second question, im not entirely sure what you mean. If you were wanted to sometimes use the u1 value and sometimes use the u2 value in your loop, you could always do:

 

<?php
if(rand(0,1)==0){
//use u1
}else{
//use u2
}
?>

 

But as i say, im not entirely sure thats what you wanted.

Link to comment
https://forums.phpfreaks.com/topic/59791-using-digits-also-for/#findComment-297370
Share on other sites

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.