x


Reducing the digits of a value

I have the following statement in my code, it displays a percentage value calculated. the value currently has about 10 decimal places coming up but i dont want any, how can i adapt this code so that it doesn't show any decimal places?

GUI.Label(new Rect(0, 100, 50, 50), System.Convert.ToString(Calculate(ace)) + "%");

more ▼

asked Jan 11 '12 at 05:16 PM

Chris 31 gravatar image

Chris 31
41 15 18 24

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Whack this line after it:

Mathf.Round(yourVariableName);
more ▼

answered Jan 11 '12 at 05:26 PM

Muzz5 gravatar image

Muzz5
1.2k 64 83 94

cheers mate :D

Jan 11 '12 at 05:30 PM Chris 31
(comments are locked)
10|3000 characters needed characters left

What is the value of ace, and what does Calculate do?

Assuming that the result is just a long decimal or float, try this...

GUI.Label(new Rect(0, 100, 50, 50), System.Convert.ToString(Mathf.Round(Calculate(ace))) + "%");

or

GUI.Label(new Rect(0, 100, 50, 50), System.Convert.ToString(Mathf.Floor(Calculate(ace))) + "%"); to just truncate the decimals

Hope this helps, -Larry

more ▼

answered Jan 11 '12 at 05:28 PM

Larry Dietz gravatar image

Larry Dietz
472 8 13 22

LOL Once again I take too long to respond, and someone beats me to it ;)

Jan 11 '12 at 05:29 PM Larry Dietz

I need the rep :) nearly at 1k!

Jan 11 '12 at 05:34 PM Muzz5
(comments are locked)
10|3000 characters needed characters left

thanks for trying dude! calculate is a method which calculates the percentage of the variable ace which can change all the time.

turns out whole number is making the % less accurate though is there a way to keep it down to just 1 or 2 decimal places?

more ▼

answered Jan 11 '12 at 05:34 PM

Chris 31 gravatar image

Chris 31
41 15 18 24

It just truncate the value to a couple of decimal places, you cn do something like this...

GUI.Label(new Rect(0, 100, 50, 50), System.Convert.ToString(Calculate(ace).Substring (0, System.Convert.ToString(Calculate(ace)).IndexOf (".")+3 ) + "%");

Or better yet, store the return of Calculate(ace) in a string variable, and use that variable in the GUI.Label to avoid calling Calculate 2 times.

I beleive that should work. -Larry

Jan 11 '12 at 05:45 PM Larry Dietz

i have saved the string as total in the other method. how would i transform it into your code?

Jan 11 '12 at 05:53 PM Chris 31

Try...

string Total = System.Convert.ToString(Calculate(ace)) GUI.Label(new Rect(0, 100, 50, 50), Total.Substring(0, Total.IndexOf (".")+3 ) + "%");

This is off the top of my head. Havent actually tried it in Unity, but beleive it should work.

-Larry

Jan 11 '12 at 06:02 PM Larry Dietz

ah thanks man :)

Jan 11 '12 at 06:13 PM Chris 31
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x24

asked: Jan 11 '12 at 05:16 PM

Seen: 436 times

Last Updated: Jan 11 '12 at 06:13 PM