rh-penguin Posted March 21, 2007 Share Posted March 21, 2007 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! Link to comment https://forums.phpfreaks.com/topic/43651-solved-howto-get-the-first-2-numbers-from-a-float-number-which-is-inside-an-array/ Share on other sites More sharing options...
Orio Posted March 21, 2007 Share Posted March 21, 2007 What do you mean by "the first 2 numbers"? The most two most left digits? Orio. Link to comment https://forums.phpfreaks.com/topic/43651-solved-howto-get-the-first-2-numbers-from-a-float-number-which-is-inside-an-array/#findComment-211921 Share on other sites More sharing options...
jitesh Posted March 21, 2007 Share Posted March 21, 2007 I am not exactly getting what do you want but still adviced to user a function number_format(); string number_format ( float number [, int decimals [, string dec_point, string thousands_sep]] ) Link to comment https://forums.phpfreaks.com/topic/43651-solved-howto-get-the-first-2-numbers-from-a-float-number-which-is-inside-an-array/#findComment-211932 Share on other sites More sharing options...
rh-penguin Posted March 21, 2007 Author Share Posted March 21, 2007 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. Link to comment https://forums.phpfreaks.com/topic/43651-solved-howto-get-the-first-2-numbers-from-a-float-number-which-is-inside-an-array/#findComment-211951 Share on other sites More sharing options...
Orio Posted March 21, 2007 Share Posted March 21, 2007 If you want to round it down, you could use floor(). Orio. Link to comment https://forums.phpfreaks.com/topic/43651-solved-howto-get-the-first-2-numbers-from-a-float-number-which-is-inside-an-array/#findComment-211952 Share on other sites More sharing options...
rh-penguin Posted March 21, 2007 Author Share Posted March 21, 2007 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! Link to comment https://forums.phpfreaks.com/topic/43651-solved-howto-get-the-first-2-numbers-from-a-float-number-which-is-inside-an-array/#findComment-211976 Share on other sites More sharing options...
rh-penguin Posted March 21, 2007 Author Share Posted March 21, 2007 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? :-\ Link to comment https://forums.phpfreaks.com/topic/43651-solved-howto-get-the-first-2-numbers-from-a-float-number-which-is-inside-an-array/#findComment-212168 Share on other sites More sharing options...
Orio Posted March 21, 2007 Share Posted March 21, 2007 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. Link to comment https://forums.phpfreaks.com/topic/43651-solved-howto-get-the-first-2-numbers-from-a-float-number-which-is-inside-an-array/#findComment-212226 Share on other sites More sharing options...
rh-penguin Posted March 21, 2007 Author Share Posted March 21, 2007 It works! Thanks so much! PS-i know that variables cant start with a number i just did it as a reference(but neways i forgot... ) Link to comment https://forums.phpfreaks.com/topic/43651-solved-howto-get-the-first-2-numbers-from-a-float-number-which-is-inside-an-array/#findComment-212313 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.