C# How to round fraction to 15

private double RoundOffset(double offset){    double value = Math.Truncate(offset);    double fraction = offset - Math.Truncate(offset);    if (fraction < 0.25) {        return value + 0.0;    } else if (fraction >= 0.25 && fraction < 0.5) {        return value + 0.25;    } else if (fraction >= 0.5 && fraction < 0.75) {        return value + 0.5;    } else if (fraction >= 0.75 && fraction < 1) {        return value + 0.75;    }    return fraction;}

Usage:

double test = 1.17; Console.WriteLine(test + ": " + RoundOffset(test));

Output:

1.17: 1.15

 

Leave a Reply

Your email address will not be published. Required fields are marked *