JayPee Posted March 16, 2012 Share Posted March 16, 2012 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 { //[email protected] - dividend //[email protected] - divisor //getarg(2) - decimal digits //return values: //Undefined if the divisor is 0 //0 if the dividend is 0 set [email protected],getarg(0); set [email protected],getarg(1); if(getarg(2)==0) set [email protected],2; else set [email protected],getarg(2); //Prefix set [email protected]$,""; if([email protected]<0) { set [email protected],abs(getarg(0)); if([email protected]$=="-") set [email protected]$,""; else set [email protected]$,"-"; } if([email protected]<0) { set [email protected],abs(getarg(1)); if([email protected]$=="-") set [email protected]$,""; else set [email protected]$,"-"; } if([email protected][email protected]) { set [email protected]_decimals$,""; for(set [email protected],0; [email protected]<[email protected]; set [email protected],[email protected]+1) { set [email protected]_decimals$,[email protected]_decimals$+"0"; } set [email protected]_result$,"1."[email protected]_decimals$; } else if([email protected]==0) return "Undefined"; else if([email protected]==0) { set [email protected]_decimals$,""; for(set [email protected],0; [email protected]<[email protected]; set [email protected],[email protected]+1) { set [email protected]_decimals$,[email protected]_decimals$+"0"; } set [email protected]_result$,"0."[email protected]_decimals$; } else { if([email protected]>[email protected]) { set [email protected]_Counter,0; //Counter set [email protected],0; //Reset value to zero set [email protected],[email protected]; //ex 10(dividend) - 3(divisor) = 7(result) do { if(([email protected]@divisor)>=0) { set [email protected],[email protected]@divisor; set [email protected]_Counter,[email protected]_Counter+1; } else break; }while([email protected]>0); set [email protected]_result$,""; set [email protected]_result$,[email protected]_result$+""[email protected]_Counter; if([email protected] >= 1) { //If the remainder is greater or equal to 1 set [email protected],0; //Start indicator set [email protected]_decimals$,""; //temporary holder of the decimal values while([email protected] < [email protected]) { set [email protected]_Counter,0; //Counter set [email protected],[email protected]*10; //Remainder([email protected]) * 10 do { if(([email protected]@divisor)>=0) { set [email protected],[email protected]@divisor; set [email protected]_Counter,[email protected]_Counter+1; } else break; }while([email protected]>0); //Append the decimal set [email protected]_decimals$,[email protected]_decimals$+""[email protected]_Counter; //If there is a remainder check the limit of decimal digit //else break the result if([email protected]>=1) set [email protected],[email protected]+1; else break; } if(getstrlen([email protected]_decimals$)>[email protected]) set [email protected]_decimals$,substr([email protected]_decimals$,0,[email protected]); else { for(set [email protected],getstrlen([email protected]_decimals$); [email protected]<[email protected]; set [email protected],[email protected]+1) { set [email protected]_decimals$,[email protected]_decimals$+"0"; } } set [email protected]_result$,[email protected]_result$+"."[email protected]_decimals$; } else { //If the remainder is equal to zero nothing to loop set [email protected]_decimals$,""; for(set [email protected],0; [email protected]<[email protected]; set [email protected],[email protected]+1) { set [email protected]_decimals$,[email protected]_decimals$+"0"; } set [email protected]_result$,[email protected]_result$+"."[email protected]_decimals$; } } else { //The divisor is greater than the dividend set [email protected]_result$,"0"; set [email protected]_decimals$,""; //Multiply the [email protected] to ten, hundred, thousand set [email protected]_multi,1; set [email protected],0; do { set [email protected],([email protected])*(pow(10,[email protected]_multi)); if([email protected]<[email protected]) set [email protected]_decimals$,[email protected]_decimals$+"0"; set [email protected]_multi,[email protected]_multi+1; }while([email protected]<[email protected]); if([email protected][email protected]) set [email protected]_decimals$,"1"; else if([email protected]>[email protected]) { while([email protected]<[email protected]) { set [email protected]_Counter,0; do { if(([email protected]@divisor)>=0) { set [email protected],[email protected]@divisor; set [email protected]_Counter,[email protected]_Counter+1; } else break; }while([email protected]>0); if([email protected]>0){ set [email protected]_decimals$,[email protected]_decimals$+""[email protected]_Counter; set [email protected]_multi,1; do { set [email protected],([email protected])*(pow(10,[email protected]_multi)); if([email protected]<[email protected]) set [email protected]_decimals$,[email protected]_decimals$+"0"; set [email protected]_multi,[email protected]_multi+1; }while([email protected]<[email protected]); } else { set [email protected]_decimals$,[email protected]_decimals$+""[email protected]_Counter; break; } set [email protected],[email protected]+1; } } if(getstrlen([email protected]_decimals$)>[email protected]) set [email protected]_decimals$,substr([email protected]_decimals$,0,[email protected]); else { for(set [email protected],getstrlen([email protected]_decimals$); [email protected]<[email protected]; set [email protected],[email protected]+1) { set [email protected]_decimals$,[email protected]_decimals$+"0"; } } set [email protected]_result$,"0."[email protected]_decimals$; } } return [email protected]refix$+""[email protected]_result$; } Quote Link to comment Share on other sites More sharing options...