Decimals in a Float

1

Stats

5,912 visits, 8,798 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 12 Dec, 2011. Last updated 19 Feb, 2019

When displaying a float, there can be a lot of decimals which may not look very nice in a highscore or otherwise. But there is a simple way to easily cut away unwanted decimals! So instead of seeing 10 decimals (ex. 1228.7292622224), you can now see two decimals (ex. 1228.72) instead!

And here is how you can do it!

Here is the basic setup I use.

The interesting stuff is in the "Set number of decimals" group, the other variables are just for show.

1.

First I create 2 local variables and a global variable.

- ValueLength (Number) will store the length of the float that I wish to change.

- ValueWithDecimal (Number) is the final variable where I store the new float with a set number of decimals.

- NumberOfDecimals (Number) is a global variable, where I set how many decimals I want to have in my final float. This variable can be changed whenever, and does not need to be a global variable.

2.

In order to cut away excess decimals, I need to convert my float into a string, and then find out how many characters (or numbers) there is, until I reach the decimals. This is easiest done, by searching for the "." character, that marks where the numbers ends, and the decimals begin.

To do this I use the following formula:

    find(str(RandomFloatValue), ".")

find()-function has two inparameter; The string I want to search, and for what kind of character I am searching for. This function returns the Index value where the character (in our case: the ".") is located.

But since our value is a float, I need to use the str()-function, which simply converts the float into a string.

3.

Now when I have where the "." begins in the string, I just need to cut out the excess decimals.

I do this by this formula:

    float(left(str(RandomFloatValue), (ValueLength + (NumberOfDecimals + 1))))

This may look confusing, but its really simple!

float()-function converts the final string into a float. This function is optional, and should only be used if you want to have a float, and not a string.

left()-function takes two parameters; a String and a integer value. The function then returns a string, that is created from the first character (index 0) and until the given index integer. As a second paramter I use the global value plus one (since I need to take one step after the "." character).

But before I use the left()-function, I once more need to convert our float value into a string value, so I use the str()-function to simply convert the float into a string, and use it as a parameter to the left()-function.

So by converting the float variable with all those decimals, into a string. I can search the string for where the decimals begin, and from there I can create a new string with the left()-function and with the parameters control how long the string should be. Then I convert the string back to a float, if needed/wished.

Link to a simple example:

http://dl.dropbox.com/u/52716812/SetDecimals.capx

  • 1 Comments

  • Order by
Want to leave a comment? Login or Register an account!