playMaker

Author Topic: Round Float with decimal places[SOLVED]  (Read 10815 times)

Alex W.

  • Playmaker Newbie
  • *
  • Posts: 13
Round Float with decimal places[SOLVED]
« 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!


Code: [Select]
// (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;
}
}
}
}
}
« Last Edit: April 09, 2020, 03:56:10 AM by jeanfabre »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Round Float with decimal places
« Reply #1 on: January 19, 2012, 05:20:38 AM »
Hi,

 Are you doing this to then save it as a string or else?

Bye,

 Jean

Alex W.

  • Playmaker Newbie
  • *
  • Posts: 13
Re: Round Float with decimal places
« Reply #2 on: January 19, 2012, 07:07:58 AM »
Hmm, good idea. I have edit the code.
Now you can store the result as String. See the first post.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Round Float with decimal places
« Reply #3 on: January 20, 2012, 01:34:01 AM »
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/

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

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

Alatriste

  • Full Member
  • ***
  • Posts: 194
Re: Round Float with decimal places
« Reply #4 on: March 30, 2020, 12:17:03 PM »
That's really handy! Should be in the Ecosystem. The round float action that already exists is quite limited.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Round Float with decimal places
« Reply #5 on: April 06, 2020, 02:51:51 AM »
Hi,

the ConvertFloatToString action shipped with PlayMaker does exactly that. Have you tried?

Bye,

 Jean

Alatriste

  • Full Member
  • ***
  • Posts: 194
Re: Round Float with decimal places
« Reply #6 on: April 06, 2020, 03:37:42 AM »
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. :)