0

I am calculating time which is float in variable, I am then converting the float to only 2 decimal points and then passing it to a string variable. I however do not require converting the float to decimal points, instead round up the float to integer. How do I achieve this?

That is for example, if timer is > 26.50f, then final timer should be 27.

Also, can we manually set the threshold? The threshold being values after the decimal point? And then the script decides which number the integer belongs to? For example, I set the threshold as .25 till .99

float 1 = 23.26 = 24
float 2 = 17.25 = 18
float 3 = 19.24 = 19
public float timer;
public string timer_string;

void Update()
{
    timer += Time.deltaTime;
    timer_string = timer.ToString("F2"); //decimal upto 2 places
    timer_string = timer.ToString("F0"); //is this the way? Since it does not round up
}
  • 1
    ```Mathf.Ceil()``` is for Unity, does this round it up? –  Jul 08 '20 at 14:22
  • 1
    @mjwills it is ambiguous, I'll agree - but OP explicitly says "round up" at least twice – Marc Gravell Jul 08 '20 at 14:23
  • @mjwills it should remain 26. –  Jul 08 '20 at 14:24
  • Did you even searched for the answer? There are hundreds of similar ones, e.g. "how to round a number in c#". – MakePeaceGreatAgain Jul 08 '20 at 14:25
  • 1
    @MarcGravell the function with ```Mathf.Ceil()``` works, if the value is greater than 0.5f, then the next value is selected as integer. –  Jul 08 '20 at 14:25
  • 1
    @HimBromBeere i did, i wanted to know if we can set it up manually. That is default is 0.5f, but can we set like a different value? –  Jul 08 '20 at 14:26
  • 1
    @Saif what does that mean? what different value? perhaps this would be better with an example that *isn't 0.5*? or a few *different* examples – Marc Gravell Jul 08 '20 at 14:27
  • Looking at `Ceiling()` will not help here, because the problem is not with rounding up. `1.4f.ToString("F0")` gives me `"1"` but `1.5f.ToString("F0")` gives me `"2"`. So, `float.ToString()` works as you wish it to work, and you have a mistake in your assumptions. What you think is happening is not what is actually happening. – Mike Nakis Jul 08 '20 at 14:27
  • 1
    @Saif you can do that by using an offset; that example suggests you *actually* want `25.25-26.2499999` to be `26`: so... add/subtract and use `Floor`/`Ceiling` – Marc Gravell Jul 08 '20 at 14:30
  • @mjwills i did not know about this. I will try this out. Thank you. –  Jul 08 '20 at 14:33
  • @MarcGravell Thanks a ton! :) ToAll: I am aware that my question would seem like a duplicate. Believe me, I did type on google to search for this and there were answers only on rounding up to integer but no one specified from which decimal point. So that I just wanted to clear it here and in my question. Peace :) –  Jul 08 '20 at 14:50

2 Answers2

3

An approach to consider would be to make use of existing Math.Round (with midpoint rounding of AwayFromZero), but effectively shift the point were rounding occurs (by addition / subtraction). This would look like:

static double RoundBasedOnCustomThreshold(double number, double customThreshold = 0.25)
{
    // customThreshold of 1 will be equivalent to Math.Floor
    if (customThreshold <= 0 || customThreshold > 1)
        throw new ArgumentException();

    return Math.Round(number + 0.5 - customThreshold, 0, MidpointRounding.AwayFromZero);
}

By adding 0.5 - customThreshold this will likely round up in the way you are looking for.

You can play with some input values and results at https://dotnetfiddle.net/OhUO3p .

mjwills
  • 23,389
  • 6
  • 40
  • 63
  • Yes actually I thought about exactly the same way adding `0.5 - threshold` and then round on that instead. (But this was 5 Minutes before sleeping :D ) – derHugo Jul 09 '20 at 05:24
1

Rounding up the the next integer is called the "ceiling", and is Math.Ceiling. So I suspect what you want is:

timer_string = Math.Ceiling(timer).ToString(...);

(for your choice of format ...)

If you actually mean some other form of rounding, then use Math.Round(timer, MidpointRounding.YourChoiceHere) instead of Math.Ceiling; for example AwayFromZero, ToPositiveInfinity, etc.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Though this still wouldn't actually answer the requested feature of customize the threshold for rounding up like OP said e.g. already ceil values above `.25` instead of `.5` (which in this specific case could be solved by first multiply by 10, ceil and then divide by 10 again) .. for more general usecases it might get tricky though e.g. round above `.237` .. would be pretty tricky to get it working for a general threshold value I guess? – derHugo Jul 08 '20 at 16:22