Playmaker Forum
PlayMaker Updates & Downloads => Share New Actions => Topic started by: Alex W. on January 18, 2012, 01:29:24 PM
-
Round Float with decimal places.
EXAMPLE:
Source Float: < 54.2486234 >
- Result Float with "None" decimal places: < 54 >
- Result Float with "One" decimal places: < 54.2 >
- Result Float with "Two" decimal places: < 54.24 >
- Result Float with "Three" decimal places: < 54.248 >
- Result Float with "Four" decimal places: < 54.2486 >
EDIT: Now you can store the result as a String!
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.
using UnityEngine;
using System.Collections;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Convert)]
[Tooltip("Round Float with decimal places.")]
public class FloatRound : FsmStateAction
{
public enum Operation
{
None,
One,
Two,
Three,
Four
}
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat floatVariable;
[UIHint(UIHint.Variable)]
public FsmFloat storeFloatResult;
[UIHint(UIHint.Variable)]
public FsmString storeStringResult;
public Operation decimalPlaces = Operation.Two;
public bool everyFrame;
public override void Reset()
{
floatVariable = null;
storeFloatResult = null;
storeStringResult = null;
decimalPlaces = Operation.Two;
everyFrame = false;
}
public override void OnEnter()
{
DoConvertFloat();
if (!everyFrame)
Finish();
}
public override void OnUpdate()
{
DoConvertFloat();
}
void DoConvertFloat()
{
if (storeFloatResult != null)
{
switch (decimalPlaces)
{
case Operation.None:
storeFloatResult.Value = Mathf.Round(floatVariable.Value);
break;
case Operation.One:
storeFloatResult.Value = Mathf.Round((floatVariable.Value)*10f)/10f;
break;
case Operation.Two:
storeFloatResult.Value = Mathf.Round((floatVariable.Value)*100f)/100f;
break;
case Operation.Three:
storeFloatResult.Value = Mathf.Round((floatVariable.Value)*1000f)/1000f;
break;
case Operation.Four:
storeFloatResult.Value = Mathf.Round((floatVariable.Value)*10000f)/10000f;
break;
default:
break;
}
}
if (storeStringResult != null)
{
switch (decimalPlaces)
{
case Operation.None:
storeStringResult.Value = floatVariable.Value.ToString ("F" + 0.0);
break;
case Operation.One:
storeStringResult.Value = floatVariable.Value.ToString ("F" + 1.0);
break;
case Operation.Two:
storeStringResult.Value = floatVariable.Value.ToString ("F" + 2.0);
break;
case Operation.Three:
storeStringResult.Value = floatVariable.Value.ToString ("F" + 3.0);
break;
case Operation.Four:
storeStringResult.Value = floatVariable.Value.ToString ("F" + 4.0);
break;
default:
break;
}
}
}
}
}
-
Hi,
Are you doing this to then save it as a string or else?
Bye,
Jean
-
Hmm, good idea. I have edit the code.
Now you can store the result as String. See the first post.
-
Hi,
Well :) Maybe then there is a more flexible way out of this.
either you can write an action implement this
http://www.csharp-examples.net/string-format-double/ (http://www.csharp-examples.net/string-format-double/)
which would expose a lot of power into how you want to format your float as a string.
or use an Action Maddox shared some time ago.
http://hutonggames.com/playmakerforum/index.php?topic=267.0 (http://hutonggames.com/playmakerforum/index.php?topic=267.0)
Be sure to check the reference provide in this thread to know all the different options available, including float rounding "##.##" and stuff like that.
Bye,
Jean
-
That's really handy! Should be in the Ecosystem. The round float action that already exists is quite limited.
-
Hi,
the ConvertFloatToString action shipped with PlayMaker does exactly that. Have you tried?
Bye,
Jean
-
Hi Jean,
No, it didn't occur to me that the action you mentioned would do the trick as well. :( Now I know for the future. :)