x


Limiting digits in GUI displayed variables (or "snap to" GUI slider).

I have a metronome controlled by a GUI slider in my game. I have then displayed the value of the slider variable using GUI.Label. This works great, only the variable often has way to many decimals (i want it to have none). So obscure values come up for the value. Is there a way to preferably create a "snap to" GUI slider or just limit the no. of decimals to be displayed on the label? Any help will be greatly appreciated. Oh and I used variable.ToString() for displaying the variable in the GUI.

more ▼

asked Sep 07 '10 at 09:14 PM

Will 7 gravatar image

Will 7
109 6 8 15

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

1 answer: sort voted first
variable.ToString("F0");

will specify 0 decimal places or the string (F2 will be 2 decimal places, etc. in case you ever need to do that)

To answer your comment - if you want it to snap to every 5, in your specific case:

c#

variable = (((int)(variable - 3)) / 5) * 5;

js

var temp : int = (variable - 3);
variable = (temp / 5) * 5;

Should do it

more ▼

answered Sep 07 '10 at 09:22 PM

Mike 3 gravatar image

Mike 3
30.7k 10 67 255

Thankyou, i was wondering what the parentheses were for...

Sep 07 '10 at 09:27 PM Will 7

I just noticed that sometimes values such as 153 come up and then the next value down may be 148 (or alike). So 150 (for instance) as a metronome speed is unobtainable. Would there be anyway to as i mentioned create a "snap-to" GUI Slider. But this should be sufficient for now.

Sep 07 '10 at 09:34 PM Will 7

Added some basic code for snapping to increments of 5

Sep 07 '10 at 10:21 PM Mike 3

Thanks for the reply, but which variables do I swap the "variable" and "temp" with? Do I put it in "function Update () {}"?

Sep 10 '10 at 02:47 PM Will 7

All i can see it doing is removing 3 from "variable"?

Sep 10 '10 at 02:48 PM Will 7
(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:

x3811
x3570
x849
x56
x19

asked: Sep 07 '10 at 09:14 PM

Seen: 1875 times

Last Updated: Sep 07 '10 at 09:14 PM