 |
chamenas Wizard

Joined: 26 Mar 2008 Posts: 1547
|
Posted: Wed Jan 21, 2009 10:51 pm
Trying to limit Float to 2 decimal places [Solved] |
Is there an a way to limit the decimals retained after float? Seems like it should be as easy as %float(number of decimals, number) but it's not listed as an argument.
Edit:
Did a search, found %format, I think it's odd that it's not inherent in float. However, have a different problem:
| Code: |
<trigger priority="460" regex="true" id="46">
<pattern>^Spell: permanency : modifies none by \d+ for (\d+) cycles, \(\d+ (?:1/2 )?hours\)$</pattern>
<value>$Time_LeftC=%1
$Time_LeftS=$Time_LeftC*41
$Time_LeftM=%float($Time_LeftS)/60
$Time_LeftH=%float($Time_LeftM)/60
%format($Time_LeftH,2)
#sub "Permanency Cooldown : "$Time_LeftH" Real Life hours remaining."</value>
</trigger>
|
When it triggers, CMUD freezes. This happened AFTER adding the %format($Time_LeftH,2)
Edit 2:
Fixed with Taz's help
$Time_LeftH=%format($Time_LeftH,2)
but it doesn't keep it to two decimal places... doesn't look like it's doing anything. |
|
_________________ Listen to my Guitar - If you like it, listen to more
Last edited by chamenas on Mon Jan 26, 2009 1:01 am; edited 2 times in total |
|
|
 |
Taz GURU
Joined: 28 Sep 2000 Posts: 1395 Location: United Kingdom
|
Posted: Thu Jan 22, 2009 1:25 am |
$Time_LeftH=%format($Time_LeftH,2)
|
|
_________________ Taz :) |
|
|
 |
chamenas Wizard

Joined: 26 Mar 2008 Posts: 1547
|
Posted: Thu Jan 22, 2009 3:06 am |
Thanks, it didn't move it to 2 decimal places though so I suppose I don't know how to use format.
|
|
|
|
 |
charneus Wizard

Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Thu Jan 22, 2009 3:35 am |
It actually should be the other way around:
$Time_LeftH=%format(2, $Time_LeftH)
That'll keep it at 2 decimal places.
Charneus |
|
|
|
 |
chamenas Wizard

Joined: 26 Mar 2008 Posts: 1547
|
Posted: Thu Jan 22, 2009 4:55 am |
Thanks, works like a charm. Bonus question: Is there any way to isolate the two decimal places? So I could have, like...
8.50 hours, and have it chop the two so I can do the math and have 8.30 hours become
3 hours and 30 minutes ? |
|
|
|
 |
Fang Xianfu GURU

Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Thu Jan 22, 2009 7:43 am |
Unfortunately, it seems that CMUD's modulus function doesn't work with floats - the way you would do this if it did is by returning the 8.5 mod 1, which gives you the remainder when dividing 8.5 by 1 - 0.5. You can then multiply that number by 60 to get the number of minutes. Then finally you round the 8.5 down to get the 8 out.
But with CMUD's modulus function only taking integers, I'm not sure. This'd be dead easy in Lua, just fyi, because it has a function to do exactly this - math.modf takes one number and returns two, the integer part and the floating-point part. |
|
|
|
 |
gamma_ray Magician
Joined: 17 Apr 2005 Posts: 496
|
Posted: Thu Jan 22, 2009 12:34 pm |
What I always did was %round(@number*100)/100, to split out hours and minutes you could use %round(@number) for the hours and %mod(%round(@number*100),100)*60/100 to get the minutes.
|
|
|
|
 |
chamenas Wizard

Joined: 26 Mar 2008 Posts: 1547
|
Posted: Thu Jan 22, 2009 12:50 pm |
I'll try what gamma has proposed, if it doesn't work then maybe I'll have to resort to lua. I should be able to understand it, but I don't really use it much.
Edit:
Gamma's suggestion worked. I figured it would be some sort of trick with numbers and math, thanks.  |
|
|
|
 |
|
|
|