Double shown with no decimals - how to prevent it from rounding up?

Hi there :slight_smile:

I have a problem that I hope someone in here can help me with.
In my current project I am using a lot of doubles.
I have one particular value (population) that rise by 0.1 per second, but i want to show it without decimals, since you can’t have half a human as population :stuck_out_tongue:
The problem however is that it rounds 0.6 to 1, showing the player that they have an unassigned citizen without actually having one.
How can i prevent this round up?
Mathf.floor don’t seem to work for doubles, but is there some other way to achieve the same goal?

Why do you represent the number of citizens with a double? There are two things you might want to reconsider:

  • Unity prefers the use of floats and they have enough precision to represent almost all use cases for games. If you require double precision for something specific you also run into problems of having to convert between different data structure at one point or the other.
  • A count of things is just better represented by a simple int. You said it yourself: there are no 0.5 people. Instead, you should increase the population every 10 seconds by one. This will probably make things easier.

After you’ve switched your data structures, you might have already solved your problems, or you can ask another question about how to calculate the values you need or display different statistics.

You can try System.Math.Floor instead. tried Math.Floor Method (System) | Microsoft Learn

There are several types of rounding when it comes to floating point numbers. Mathf.RoundToInt will return the nearest integer, this is not what you want and should be the default behavior if you try to convert to an integer. Then there are Mathf.CeilToInt and Mathf.TruncToInt. A ceil will return the closest integer greater to or equal to the float, so Mathf.CeilToInt(10.1f) will return 11. Again, not what you want but something you should know about. Finally there is Mathf.FloorToInt, which will return the nearest integer equal or less than the float. So Mathf.FloorToInt(10.1) should return 10. This is what you want.

Also, doubles aren’t often used in game development. On many CPUs there’s no real performance difference between floats and doubles (or at least I should say on x86, I have no idea about architectures like ARM) other than the extra memory requirements, however floats are more than adequate for virtually anything you’ll need. You should probably be using floats unless you have a real reason not to.

Thank you for your answers guys! :slight_smile:

The reason that I’m using doubles is because my game contains some key-aspects related to the incremental game genre. This means that some values will go up to VERY high numbers (as in 1.000.000.000.000.000.000.000 high numbers). Right now I dont need numbers close to this, but when I started on my project - which was also when i started programming - I read that doubles was the best variable for this task. Honestly I have no idea if that’s the case, or if I just as well could’ve used floats by converting bigger numbers into something else? (Like 1.000.000.000 = 1B - had another post about that some time ago, but still not sure on the subject)
But using floats would definitely be easier if they’re just as good, and if they also increase performance it’s a win/win.
The reason I’m not using integers is because numbers will contain decimals… I just dont want the player to see them in all cases.