Jump to content

[SOLVED] howto get the first 2 numbers from a float-number which is inside an array


rh-penguin

Recommended Posts

hi,

 

I am making an app in which numbers suplied by the user get divided, added etc. The result displays a floting-point-number.

I need to get the first 2 numbers of that result(which is inside a variable) to pass on onto a different fuction but i dont know how to do this.

 

Could someone please tell me how this could be done?

 

Thanks!

 

I mean if i get a float like this: 24.666669092 in the variable i want to take that variable and cut it. So i would only get the first 2 digits starting from the left which in this case would be (24).

 

I think the number_format(); function should work. I cant test it right now as im at college but will do in bout 3-4 hours.

 

If there are other fuctions or other ways which this can be done, plz let me know. I just recently started learning php so i dont know that much.

 

Thanks for the stuff so far.

That should work perfectly!

I scrolled past it the other day looking at the round() func.I was trying to do it with round() but something wasnt right. Neways, floor() should work just as i want it too.

Thanks for the help.

 

PS-i'll put this topic as solved when i get back home and figure this out, again, thanks alot!

I get the floor(); function and it works ina code like this

<?php
$num = 21.99999;
$rounded_num = $num;
$rounded_num = floor($rounded_num);
echo("$rounded_num");
?>

BUT, if i try and use it in my app it doesnt want to work. Here is the script which i've wrote,

<?php
            $bandwith = $_POST['bandwith'];
            $1 = $_POST['1'];

            if( is_numeric( $bandwith ) && is_numeric( $1 ) )

{

            if     ( $1 <= 3000)
            {
                        $1 = 24;
                        $result = $bandwith / $1;
            }
            
		elseif ( $1 <= 4000)
            {
                        $1 = 32;
                        $result = $bandwith / $1;
            }

		else
            {
		$result = $bandwith / $1;
		$1 = floor($1);
		}


}

echo("The result: <u><b>$result</b></u>");

?>

 

Can someone please tell me what im doing wrong? :-\

 

You can't use a variable that starts with a number. First char must be either a alphabetical letter, or an underscore.

Second, you don't need to floor the variable you are flooring, you need to floor $result.

 

<?php
            $bandwith = $_POST['bandwith'];
            $var = $_POST['1'];

            if( is_numeric( $bandwith ) && is_numeric( $var ) )

{

            if     ( $var <= 3000)
            {
                        $var = 24;
                        $result = $bandwith / $var;
            }
            
		elseif ( $var <= 4000)
            {
                        $var = 32;
                        $result = $bandwith / $var;
            }

		else
            {
		$result = $bandwith / $var;
		}


}

$result = floor($result);
echo("The result: <u><b>$result</b></u>");

?>

 

 

Orio.

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.