Jump to content

Recommended Posts

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;

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

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.

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.

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.

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

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.

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);

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.

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

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.

If you're not English, you will get a problem  :D

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.

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.