Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-22-2004, 10:38 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
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.
__________________
Skyld
Reply With Quote
  #2  
Old 12-22-2004, 11:21 PM
Slash-P2P Slash-P2P is offline
Banned
Join Date: May 2004
Location: Burning Blade
Posts: 941
Slash-P2P is on a distinguished road
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?
Reply With Quote
  #3  
Old 12-22-2004, 11:28 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Well I wrote mine in shorter code than it is, but then expanded it for use here.
__________________
Skyld
Reply With Quote
  #4  
Old 12-22-2004, 11:28 PM
Lance Lance is offline
dark overlord
Lance's Avatar
Join Date: Sep 2003
Location: Space Jam Mountain
Posts: 5,072
Lance is on a distinguished road
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);
}
}

Reply With Quote
  #5  
Old 12-22-2004, 11:32 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
I suppose both examples make whole amounts more sense, really. :P
__________________
Skyld
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 03:13 AM.


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