Jump to content

get string floating value in division


JayPee

Recommended Posts


  • Group:  Members
  • Topic Count:  47
  • Topics Per Day:  0.01
  • Content Count:  633
  • Reputation:   78
  • Joined:  11/14/11
  • Last Seen:  

Script Name: strDivide

Description: Allows you to divide two integers. It returns a floating value. Note: the floating value is in string datatype.

Version:

  • 1.1 - added division of negative numbers and some variable cleanups

Parameters(should be in order):

  • dividend - this should be a number not a string
  • divisor - this should be a number not a string
  • number of decimal digits.

Note: You must have this source modification http://rathena.org/board/topic/60307-additional-script-command-for-math-function-absx/page__pid__85826#entry85826. Since absolute script command is not yet added in rathena SVN.

function script strDivide {
//.@dividend - dividend
//.@divisor - divisor
//getarg(2) - decimal digits

//return values:
//Undefined if the divisor is 0
//0 if the dividend is 0

set .@dividend,getarg(0);
set .@divisor,getarg(1);

if(getarg(2)==0)
set .@limit,2;
else
set .@limit,getarg(2);


//Prefix
set .@prefix$,""; 
if(.@dividend<0)
{
set .@dividend,abs(getarg(0));
if(.@prefix$=="-")
set .@prefix$,"";
else
set .@prefix$,"-";
}
if(.@divisor<0)
{
set .@divisor,abs(getarg(1));
if(.@prefix$=="-")
set .@prefix$,"";
else
set .@prefix$,"-";

}

if(.@dividend==.@divisor)
{ 
set .@str_decimals$,"";
for(set .@i,0; .@i<.@limit; set .@i,.@i+1)
{
set .@str_decimals$,.@str_decimals$+"0";
} 
set .@str_result$,"1."+.@str_decimals$;
}
else if(.@divisor==0)
return "Undefined";
else if(.@dividend==0)
{
set .@str_decimals$,"";
for(set .@i,0; .@i<.@limit; set .@i,.@i+1)
{
set .@str_decimals$,.@str_decimals$+"0";
}
set .@str_result$,"0."+.@str_decimals$;
}
else
{ 
if(.@dividend>.@divisor)
{ 
set .@tmp_Counter,0; //Counter
set .@result,0; //Reset value to zero

set .@result,.@dividend; //ex 10(dividend) - 3(divisor) = 7(result)
do
{
if((.@result-.@divisor)>=0)
{
set .@result,.@result-.@divisor;
set .@tmp_Counter,.@tmp_Counter+1;
}
else
break;
}while(.@result>0);

set .@str_result$,"";
set .@str_result$,.@str_result$+""+.@tmp_Counter;

if(.@result >= 1)
{
//If the remainder is greater or equal to 1 

set .@start,0; //Start indicator
set .@str_decimals$,""; //temporary holder of the decimal values
while(.@start < .@limit)
{
set .@tmp_Counter,0; //Counter
set .@result,.@result*10; //Remainder(.@result) * 10
do
{
if((.@result-.@divisor)>=0)
{
set .@result,.@result-.@divisor;
set .@tmp_Counter,.@tmp_Counter+1;
}
else
break;
}while(.@result>0);

//Append the decimal
set .@str_decimals$,.@str_decimals$+""+.@tmp_Counter;

//If there is a remainder check the limit of decimal digit
//else break the result
if(.@result>=1)
set .@start,.@start+1;
else
break; 


}
if(getstrlen(.@str_decimals$)>=.@limit) 
set .@str_decimals$,substr(.@str_decimals$,0,.@limit-1);
else
{
for(set .@i,getstrlen(.@str_decimals$); .@i<.@limit; set .@i,.@i+1)
{
set .@str_decimals$,.@str_decimals$+"0";
}
}

set .@str_result$,.@str_result$+"."+.@str_decimals$;
}
else
{
//If the remainder is equal to zero nothing to loop

set .@str_decimals$,"";
for(set .@i,0; .@i<.@limit; set .@i,.@i+1)
{
set .@str_decimals$,.@str_decimals$+"0";
}
set .@str_result$,.@str_result$+"."+.@str_decimals$;
} 
}
else
{
//The divisor is greater than the dividend
set .@str_result$,"0";
set .@str_decimals$,"";
//Multiply the .@dividend to ten, hundred, thousand
set .@tmp_multi,1;
set .@start,0;

do
{
set .@result,(.@dividend)*(pow(10,.@tmp_multi));
if(.@result<.@divisor)
set .@str_decimals$,.@str_decimals$+"0";

set .@tmp_multi,.@tmp_multi+1; 
}while(.@result<=.@divisor); 

if(.@result==.@divisor)
set .@str_decimals$,"1";
else if(.@result>.@divisor) 
{
while(.@start<.@limit)
{
set .@tmp_Counter,0; 
do
{
if((.@result-.@divisor)>=0)
{
set .@result,.@result-.@divisor;
set .@tmp_Counter,.@tmp_Counter+1;
}
else
break; 
}while(.@result>0);

if(.@result>0){
set .@str_decimals$,.@str_decimals$+""+.@tmp_Counter; 
set .@tmp_multi,1;
do
{
set .@result,(.@result)*(pow(10,.@tmp_multi));
if(.@result<.@divisor)
set .@str_decimals$,.@str_decimals$+"0";
set .@tmp_multi,.@tmp_multi+1; 
}while(.@result<.@divisor); 
}
else
{ 
set .@str_decimals$,.@str_decimals$+""+.@tmp_Counter;
break;
}
set .@start,.@start+1;
}
}

if(getstrlen(.@str_decimals$)>=.@limit) 
set .@str_decimals$,substr(.@str_decimals$,0,.@limit-1);
else
{ 
for(set .@i,getstrlen(.@str_decimals$); .@i<.@limit; set .@i,.@i+1)
{
set .@str_decimals$,.@str_decimals$+"0";
}
}

set .@str_result$,"0."+.@str_decimals$;
}
}
return .@prefix$+""+.@str_result$;
}

Link to comment
Share on other sites

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.   Paste as plain text instead

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