How do i prevent a double from showing the whole number, before it's there when using .ToString("0")

Hi guys

I’m having an issue in the project that I’m working on.
I have some resource values that are slowly ticking up as you’re gathering them.
I’m using a double, but when displaying it in game I’m showing them without decimals using .ToString(“0”)

For some reason when running the game the display shows the full number (1,2,3,4,etc.) before it’s actually gathered - my estimate is that it rounds up, once it reached X.5.

I first realized this when implementing a resource that accumulate much slower than the normal resources (a 0.1/sec rate).
It feels really clunky that you have to wait about 5 seconds, before being able to buy the object you want to buy, when the display tells you that you’re able to.

Is there a way to fix this?

Thank you for reading :slight_smile:

You should use the Mathf.Floor() function on your floating point value first, before you call ToString(“0”) on it.

 float thingy = 1.9f;
 string display = Mathf.Floor(thingy).ToString("0");

See also Mathf.Ceiling() and Mathf.Round().

2 Likes

Thank you I’ll try that!

What do the 1.9f do?
What would happen if i wrote 0.9f or 2.9f instead?

Is there a “better” or more beginner friendly place to look up c#/unity functions other than the Unity API?
I’m often times having a hard time understanding the API description.

When I looking up mathf.floor at https://docs.unity3d.com/ScriptReference/Mathf.Floor.html
I am struggling to understand what their example code actually do.

Just implemented your suggestion and it works! Thank you :slight_smile:
Did not set the float (in my case a double) to 1.9f; but just 0; but I don’t know if that was just to exemplify on your behalf cause it seems to work :slight_smile:

1 Like

Yes, that 1.9f was just an arbitrary value I put into the float to show how the rounding would work in that trivial example. It has no impact on the functioning of the Floor function.

Glad it works for you!!

MSDN is good for looking up non Unity C# stuff. However its less friendly then the Unity reference material.

1 Like