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