SphereCommunity
Floats and Percentage - Printable Version

+- SphereCommunity (https://forum.spherecommunity.net)
+-- Forum: Sphere 0.56d (/Forum-Sphere-0-56d)
+--- Forum: Script Help (/Forum-Script-Help)
+--- Thread: Floats and Percentage (/Thread-Floats-and-Percentage)



Floats and Percentage - Barnabus - 10-02-2012 10:10 AM

This is laughable I know.. It works perfect but can anyone tell me what I have done wrong, because I dont think I should be dividing by a million.
Code:
[FUNCTION f_Percentage] //f_Percentage <%>,<WholeNumber>
FLOAT.ValueA=<EVAL <ARGV[0]>>
FLOAT.ValueB=<EVAL <ARGV[1]>>
LOCAL.PercentageChop = <FLOATVAL (<FLOATVAL <FLOAT.ValueA> / 1000000>)>
LOCAL.PercentageCalc = <FLOATVAL (<FLOAT.ValueB> * <LOCAL.PercentageChop>)/10>
LOCAL.BacktoWholeNumber = <EVAL <LOCAL.PercentageCalc>/10>
SAY @Percentage <EVAL <ARGV[0]>> Percent of <EVAL <ARGV[1]>> is <EVAL <LOCAL.BacktoWholeNumber>>
RETURN <EVAL <LOCAL.BacktoWholeNumber>>



RE: Floats and Percentage - Barnabus - 10-02-2012 10:06 PM

DarkSun Pointed out ***

If <ARVGV[0]> is 25
FLOAT.ValueA=<EVAL <ARGV[0]>> = 25.0

and <FVAL <FLOAT.ValueA>> = 25,000,00.0

Dividing by 1kk = 2.5 hence a further divison by 10 to get the .25 (25%)

<FVAL <FLOAT.ValueA>> = 25,000,00.0 <<< This dont make sense ...

If I Evaluate 2.0 I get 20 If I evaluate 20 I get 20 so when I Float Evaluate 2.5 I would expect 2.5

Is it supposed to be like how it is ?


RE: Floats and Percentage - Extreme - 10-02-2012 11:59 PM

What do you really want? A percentage function?
Do you want decimal values or integers is okay?
PHP Code:
[FUNCTION F_PERCENTAGE// F_PERCENTAGE 10,100 = 10%
RETURN <EVAL (<ARGV[0]>*<ARGV[1]>)/100



RE: Floats and Percentage - Shaklaban - 10-03-2012 01:36 AM

you can also use muldiv for percantage like

local.perc = <muldiv <argn1> 10 100> //returns %10 of argn1

http://msdn.microsoft.com/en-us/library/windows/desktop/aa383718(v=vs.85).aspx


RE: Floats and Percentage - Barnabus - 10-03-2012 07:42 AM

Extreme and Shaklaban - Thanks guys though Its not a percentage function I need. Its the floats I am confused about.
(Value/100*Percentage) is fine in integers, though I wanted to do it the other way for example 25% of 100 = (100*0.25)
Now casting an integer to a float in sphere is slightly puzzling. Take the little function below.
Code:
[FUNCTION f_floatYerBoat]
FLOAT.Whatever=<FVAL <ARGV[0]>>
SAY <FLOAT.Whatever>
If I input .f_floatYerBoat 20; I would expect to see 20.0 returned but instead it returns 2.00000, meaning its not simply returned my whole number as a decimal, instead it has divided it by 10 then returned it as a decimal.
So I was wondering am I using FLOAT and FVAL correctly ?


RE: Floats and Percentage - RanXerox - 10-03-2012 08:13 AM

FVAL is just an evaluation of the expression that returns a string with the decimal place moved one point to the left... Its a cosmetic function to make it easier to display a number in a dialog. so when you do <FVAL 20> the output is 2.0 and if you do <FVAL 20.0> you get 20.0 and if you do <FVAL 20.000> you get 2000.0


RE: Floats and Percentage - Barnabus - 10-03-2012 09:05 PM

Thanks RanXerox, now I understand I thought It was numerical like float/double

Thanks