wkdw1ll1ams Posted August 2, 2012 Share Posted August 2, 2012 Let's say i have this code: <? $num1 = 10; $num2 = 10; echo $num1 * $num2; ?> How do i put a "0.10" behind $num1 so it will become 0.10? I was thinking something like this: <? $num1 = 10; $num2 = "0." $num3 = $num2,10; echo $num1 * $num3; ?> Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/ Share on other sites More sharing options...
Jessica Posted August 2, 2012 Share Posted August 2, 2012 number_format Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366301 Share on other sites More sharing options...
Psycho Posted August 2, 2012 Share Posted August 2, 2012 Hmm, your question, as stated, doesn't make sense. You state How do i put a "0.10" behind $num1 so it will become 0.10? If you put a "0.10" behind $num1 it would result in 100.10. I *think* what you are wanting to do is treat $num1 as a percentage. So, $num1 * $num2 = (0.10) * 10 = 1. Is that correct? Then this is simple math and not a coding issue: $num1 = 10; $num2 = 10; $result = ($num1/100) * $num2; echo $result; Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366328 Share on other sites More sharing options...
hakimserwa Posted August 2, 2012 Share Posted August 2, 2012 when you wrap number inside "" then they become strings so mathematical calculation wont affect them Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366341 Share on other sites More sharing options...
Jessica Posted August 2, 2012 Share Posted August 2, 2012 when you wrap number inside "" then they become strings so mathematical calculation wont affect them Not true, PHP has loose typing. Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366345 Share on other sites More sharing options...
Barand Posted August 2, 2012 Share Posted August 2, 2012 when you wrap number inside "" then they become strings so mathematical calculation wont affect them Not true, PHP has loose typing. In fact you can use arithmetic operator on non-numeric characters eg $a = 'A'; for ($i=0; $i<26; $i++) echo $a++; //-> ABCDEFGHIJKLMNOPQRSTUVWXYZ Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366349 Share on other sites More sharing options...
wkdw1ll1ams Posted August 2, 2012 Author Share Posted August 2, 2012 Hmm, your question, as stated, doesn't make sense. You state How do i put a "0.10" behind $num1 so it will become 0.10? If you put a "0.10" behind $num1 it would result in 100.10. I *think* what you are wanting to do is treat $num1 as a percentage. So, $num1 * $num2 = (0.10) * 10 = 1. Is that correct? Then this is simple math and not a coding issue: $num1 = 10; $num2 = 10; $result = ($num1/100) * $num2; echo $result; That is for adding a percentage, I'm trying find the percentage of a number. for example, what is 50% of 100: 0.5x100 = 50 I think I've found what i was looking for anyway. $number3 = 50; $number4 = 100; $number3 /= 100; $number1 = $number3 * $number4 echo "<script>alert('$number3 is $number1 as a percentage')</script>"; } ?> Because I'm a beginner php programmer, i can't find a better way. P.S that code works. Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366350 Share on other sites More sharing options...
Jessica Posted August 2, 2012 Share Posted August 2, 2012 100% of 50 is 50. You're not being clear at all. 50% of 100 is 50. Any number divided by 100 is a percentage. Then you use number_format (is there an echo in here?) to format the leading 0s and decimal places. If you want it displayed with the %, you must multiply by 100. Like your second example. Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366353 Share on other sites More sharing options...
wkdw1ll1ams Posted August 2, 2012 Author Share Posted August 2, 2012 100% of 50 is 50. You're not being clear at all. 50% of 100 is 50. Any number divided by 100 is a percentage. Then you use number_format (is there an echo in here?) to format the leading 0s and decimal places. If you want it displayed with the %, you must multiply by 100. Like your second example. Sorry, Could you post some code of how you would do it so i have a better understanding of this number_format thing. When i started php yesterday, i was surprised of the similarities of c++ (I'm a cpp programmer) so i guess php will be easy to understand because of this. Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366357 Share on other sites More sharing options...
Jessica Posted August 2, 2012 Share Posted August 2, 2012 RTFM Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366358 Share on other sites More sharing options...
wkdw1ll1ams Posted August 2, 2012 Author Share Posted August 2, 2012 RTFM Keep your knickers on. Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366359 Share on other sites More sharing options...
KevinM1 Posted August 2, 2012 Share Posted August 2, 2012 If you're familiar with C++, there's no reason why PHP should be challenging for you, considering PHP is widely regarded as one of the easiest languages to learn. Moreover, you've been given links which go directly to the number_format (look, another one) documentation, which itself has both the function signature as well as a very clear code example that looks at 3 ways to actually use the function. I'm not sure what else you require. We're not going to write your code for you, and you have been given textbook example code. Finally, jesirose is female, so the proper insult would be "internet tough girl/woman." Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366360 Share on other sites More sharing options...
wkdw1ll1ams Posted August 2, 2012 Author Share Posted August 2, 2012 If you're familiar with C++, there's no reason why PHP should be challenging for you, considering PHP is widely regarded as one of the easiest languages to learn. Moreover, you've been given links which go directly to the number_format (look, another one) documentation, which itself has both the function signature as well as a very clear code example that looks at 3 ways to actually use the function. I'm not sure what else you require. We're not going to write your code for you, and you have been given textbook example code. Finally, jesirose is female, so the proper insult would be "internet tough girl/woman." When the hell did i ask you to write my code, trying to be a smartass won't help my situation. I've found what i was looking for so you might aswell close this. Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366361 Share on other sites More sharing options...
Psycho Posted August 2, 2012 Share Posted August 2, 2012 That is for adding a percentage, I'm trying find the percentage of a number. No. You obviously didn't really read my response or even try it. for example, what is 50% of 100: 0.5x100 = 50 I think I've found what i was looking for anyway. $number3 = 50; $number4 = 100; $number3 /= 100; $number1 = $number3 * $number4 echo "<script>alert('$number3 is $number1 as a percentage')</script>"; } ?> What I provided was exactly what you just wrote, but mine was more efficient. I don't know why you changed the sample inputs in your later example - it only confuses the situation. //My code $num1 = 50; $num2 = 100; $result = ($num1/100) * $num2; //$result will be 50, i.e. 50% of 100 //Your code $number3 = 50; $number4 = 100; $number3 /= 100; $number1 = $number3 * $number4; //$number1 will be 50 (i.e. 50% of 100); Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366373 Share on other sites More sharing options...
hakimserwa Posted August 3, 2012 Share Posted August 3, 2012 when you wrap number inside "" then they become strings so mathematical calculation wont affect them Not true, PHP has loose typing. In fact you can use arithmetic operator on non-numeric characters eg $a = 'A'; for ($i=0; $i<26; $i++) echo $a++; //-> ABCDEFGHIJKLMNOPQRSTUVWXYZ ithink it should be some arithmetic operators. Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366640 Share on other sites More sharing options...
KevinM1 Posted August 3, 2012 Share Posted August 3, 2012 when you wrap number inside "" then they become strings so mathematical calculation wont affect them Not true, PHP has loose typing. In fact you can use arithmetic operator on non-numeric characters eg $a = 'A'; for ($i=0; $i<26; $i++) echo $a++; //-> ABCDEFGHIJKLMNOPQRSTUVWXYZ I believe that works because PHP uses ASCII characters, which have a contiguous range with the alphabet character codes. Basically, since each character has an underlying numerical code, it's treated as an integer under the hood, so arithmetic operators will work on them. It's less to do with loose typing and more to do with being built on C (which is statically typed, but handles character arithmetic the same way). Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366643 Share on other sites More sharing options...
jazzman1 Posted August 3, 2012 Share Posted August 3, 2012 ithink it should be some arithmetic operators. Really ? $a = 'Z'; for ($i=0; $i<26; $i++) echo $a--; $a = 26; for ($i=0; $i<26; $i++) echo $a--; Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366657 Share on other sites More sharing options...
KevinM1 Posted August 3, 2012 Share Posted August 3, 2012 ithink it should be some arithmetic operators. Really ? $a = 'Z'; for ($i=0; $i<26; $i++) echo $a--; $a = 26; for ($i=0; $i<26; $i++) echo $a--; Hehe. Yeah, you've proven us wrong. To be fair, I never had to increment characters in PHP, so when Barand said it worked, I figured that was because PHP is written in C and simply borrowed that functionality. Learn something new every day. Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366669 Share on other sites More sharing options...
hakimserwa Posted August 3, 2012 Share Posted August 3, 2012 so how do we get to know the numeric values in alphabetical characters? Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366679 Share on other sites More sharing options...
jazzman1 Posted August 3, 2012 Share Posted August 3, 2012 If you're not English, you will get a problem http://php.net/manual/en/function.chr.php Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366681 Share on other sites More sharing options...
hakimserwa Posted August 4, 2012 Share Posted August 4, 2012 If you're not English, you will get a problem http://php.net/manual/en/function.chr.php surely iam abit confused with chr() can you give me some ways of using it if you dont mind. Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366739 Share on other sites More sharing options...
jazzman1 Posted August 4, 2012 Share Posted August 4, 2012 If you're not English, you will get a problem http://php.net/manual/en/function.chr.php surely iam abit confused with chr() can you give me some ways of using it if you dont mind. @hakim everything is math in the computer's world and in the real life too, I think.. Did you see the examples of php.net ? There is a lot of tutorials and good explanations about ASCII in the web, just google it. Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366759 Share on other sites More sharing options...
hakimserwa Posted August 4, 2012 Share Posted August 4, 2012 thanks mate i will try make a research on it Quote Link to comment https://forums.phpfreaks.com/topic/266601-put-a-number-behind-a-string/#findComment-1366840 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.