Jump to content
  • 0

Show Decimals in Script


Question

Posted

Hello,

I know that rAthena doesn't support decimal variables, but I would like only display it.

For eg:

set Something, 5/2; // It will be two

So:

dispbottom "There are "+Something+" ";

And the result: "There are 2.5"

I only want to display decimal in messages, and with decimals of 2 digits (eg. 2.12)

 

Regards~

1 answer to this question

Recommended Posts

  • 1
Posted (edited)

You can try this script:

function	script	DivToDecimal	{
	.@x = getarg(0);
	.@y = getarg(1);
	
	.@precision = 2; // The amount of digits to keep after the dot
	.@mod = pow(10, .@precision);
	.@left = .@x / .@y;
	.@right = (.@x * .@mod / .@y) % .@mod;
	.@result$ = "" + .@left;
	
	if (!.@right)
		return .@result$;
	
	// Removes trailing zeroes, for example:
	// With .@zeroes set to 0, 5 / 2 will output 2.5
	// With .@zeroes set to 1, 5 / 2 will output 2.50
	.@zeroes = 0;
	
	for (.@i = 0; .@i < .@precision; .@i++) {
		.@digit = .@right % 10;
		.@right = .@right / 10;
		
		if (!.@zeroes) {
			if (.@digit == 0)
				continue;
			
			.@output$ = insertchar(.@output$, .@digit + "", 0);
			.@zeroes = 1;
		}
		else {
			.@output$ = insertchar(.@output$, .@digit + "", 0);
		}
	}
	
	return .@result$ + "." + .@output$;
}

prontera,154,161,0	script	TestNPC	567,{
	dispbottom "Your number is: " + callfunc("DivToDecimal", 5, 2);
	end;
}

Be careful with overflows if your numbers are too high (this script breaks easily, but for simple divisions it will work out fine).

Edit: If you must store the result of your division in a variable, you'll have to do some dirty tricks, for example:

Something = 5 * 100 / 2;
//...
dispbottom "There are " + callfunc("DivToDecimal", Something, 100);

 

Edited by Tokei
  • Upvote 3
  • Love 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...