# Percentage

Hi All,
I want to get the percentage result based from two values,

``` dim a,b,c as Integer a=1000000 b=-125000 c=(b/a)*100 MsgBox str(c)```

The result is -12, how to get the result for 12.5%

any helps?

Thanks
Regards,
Arief

-125000 is not 12.5% of 1000000

A as a percentage of B
Percentage = (A/B) * 100

if you think/want that a negative number can be expressed in that way,

ABS(A)/ABS(B) * 100

```dim a,b as integer dim c as double // otherwise 12,5 becomes 12 a=1000000 b=-125000 c=ABS(b/a)*100 MsgBox Format(c, "###.##")```

Also, you are storing the resulting value in an Integer, which is not an adequate type for a value that is not an integer (as Markus has pointed out in his code).

[quote=381649:@Jeff Tullin]-125000 is not 12.5% of 1000000
if you think/want that a negative number can be expressed in that way,[/quote]
Think of it as “I have \$125,000 of debt but \$1 million in assets”

Thanks for the solution, converting to double and using ABS Function, do the tricks.
Its works like a charm.

Thanks
Regards,
Arief

P.S. dont forget to add some error checking. For example if a is zero then you are in trouble. What if a or b are larger than an integer value can hold?

I think, there will be no problem.
the code only executed while the value <> empty.
And the value of B is always smaller than A.

Thanks
Regards
Arief

famous last words

[quote=381687:@Arief Sch.]I think, there will be no problem.
the code only executed while the value <> empty.
And the value of B is always smaller than A.[/quote]

A = 0
B = -100

-> Problem

Note: a zero value is NOT an empty value!

A = 5,000,000
B = 1,000,000

-> Problem

You really should use this case as an opportunity to learn about defensive programming and hardening your code

Markus, I understand your first example (A = 0), but the second I don’t know why that’s a problem, it is because the number has formatting (,) or because B is not negative?

Read up on integer, then on integer overflow.

Whenever you have a question, your first stop should be the documentation, not the forum.

P.S. there is a saying: Give a man a fish and he will be fine for a day, but teach him to fish and he never will be hungry again.

Learning to program is about learning to solve problems. So this time Im not giving you the solution (the fish) but point you in the right direction to read, learn, and find the solution yourself (teaching you to fish)

Sorry, I did read about integer and Uinteger in 32 bit apps the values are -2,147,483,648 to 2,147,483,647 (32-bit apps) and 0 to 4,294,967,295 (32-bit apps), info from here. Both are within those numbers.

I even created a test with your code above changing the values:

```Dim a,b As Integer Dim c As Double // otherwise 12,5 becomes 12 a=5000000 b=1000000 c=Abs(b/a)*100 MsgBox Format(c, "###.##")```

And still don’t see the problem. The result is 20 as expected. Thank you.

My mistake. Try it with Integer and

A = 5,000,000,000 (five billion)
B = 1,000,000,000 (one billion)