Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Decimal Places Script (https://forums.graalonline.com/forums/showthread.php?t=56571)

Skyld 12-22-2004 10:38 PM

Decimal Places Script
 
Earlier, I found myself writing a script where I needed to be able to round a value to so many decimal places. I do not believe Graal has a built-in method of doing this.
I wrote this code to do it. I hope that somebody may find it useful, either to use or to learn from.

NPC Code:
//#CLIENTSIDE
if (playerchats) {
tokenize #c;
if (strequals(#t(0),dp)) {
// ***Save the data first
setstring this.first,#t(1); //<- INPUT NUMBER TO BE ROUNDED
this.places=strtofloat(#t(2)); //<- NUMBER OF DECIMAL PLACES TO ROUND TO
// ***Split the data so that it can be taken into two parts (part1.part2)
tokenize2 .,#s(this.first);
setstring this.second_1,#t(0);
setstring this.second_2,#t(1);
// ***Then split up the second half of the data so we can work with it
setstring this.third_1,#e(0,this.places,#s(this.second_2)); // All the decimal places
setstring this.third_2,#e(0,this.places-1,#s(this.second_2)); // All the decimal places minus 1
setstring this.third_3,#e(this.places,1,#s(this.second_2)); // The decimal place after the round value
setstring this.third_4,#e(this.places-1,1,#s(this.second_2)); // The decimal place at the round value
// ***Now start reconstructing the decimal point
// ***The first instance here builds the number back up as it was
// ***The second instance builds it back as if the last decimal place has a number
// ***lower than 5 after it
// ***But first we need to check if we were given less numbers than DP specified
if (strtofloat(#s(this.third_3))>=5) setstring this.result,#s(this.second_1).#s(this.third_2)#s(t his.third_4);
else setstring this.result,#s(this.second_1).#s(this.third_2)#v(s trtofloat(#s(this.third_4))-1);
// ***Now check whether the result was valid and then set the this.result value
if (strequals(#e(strlen(#s(this.result))-2,2,#s(this.result)),-1)) setstring this.final,#s(this.first);
else setstring this.final,#s(this.result);
this.final = strtofloat(#s(this.final));
// ***And give the player the value via chattext
setplayerprop #c,#s(this.final);
}
}



It works at the moment by saying "dp <number> <places>" which will round <number> to so many decimal <places>. If you specify too many decimal <places> for the <number>, it simply returns the <number> you originally gave it.
I do realise that there is alot of unnecessary code in there, but it may, again, help somebody learn.
It returns the final value in both the string and variable `this.final', as you can most likely see.

Slash-P2P 12-22-2004 11:21 PM

NPC Code:
//#CLIENTSIDE
if (playerchats && startswith(dp,#c)) {
tokenize #c;
this.places = strtofloat(#t(2));
setplayerprop #c,#e(0,indexof(.,#t(1))+this.places,#t(1));
}


Wouldn't something like that do the same thing?

Skyld 12-22-2004 11:28 PM

Well I wrote mine in shorter code than it is, but then expanded it for use here.

Lance 12-22-2004 11:28 PM

Other than trailing zeroes being ignored, why not do it simply? Here, I'll copy your input format:

NPC Code:
//#CLIENTSIDE
if (playerchats) {
tokenize #c;
if (strequals(#t(0),dp)) {
this.num=strtofloat(#t(1));
this.maxplaces=strtofloat(#t(2));
this.newnum=int(this.num * 10^this.maxplaces + 0.5) / 10^this.maxplaces;
setplayerprop #c,#v(this.newnum);
}
}


Skyld 12-22-2004 11:32 PM

I suppose both examples make whole amounts more sense, really. :P


All times are GMT +2. The time now is 02:46 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.