Equivalent of Excel Round function

0 votes

I have a number like this: 3206.6186522022

And I have used an excel sheet formula:

ROUND(3206.6186522022;-2)

Which gave me: 3200

So how can I achieve the same in c#?

Sep 25, 2022 in Others by Kithuzzz
• 38,000 points
1,118 views

1 answer to this question.

0 votes

Here's a sample LINQPad program that demonstrates one way to do it. I tested this by running all those numbers through Excel to verify that it behaves the same way:

void Main()
{
    Verify(ROUND(3206.618652, 2), 3206.62).Dump();
    Verify(ROUND(3206.618652, 1), 3206.6).Dump();
    Verify(ROUND(3206.618652, 0), 3207).Dump();
    Verify(ROUND(3206.618652, -1), 3210).Dump();
    Verify(ROUND(3206.618652, -2), 3200).Dump();

    Verify(ROUND(3207.618652, 2), 3207.62).Dump();
    Verify(ROUND(3207.618652, 1), 3207.6).Dump();
    Verify(ROUND(3207.618652, 0), 3208).Dump();
    Verify(ROUND(3207.618652, -1), 3210).Dump();
    Verify(ROUND(3207.618652, -2), 3200).Dump();

    Verify(ROUND(3205.618652, 2), 3205.62).Dump();
    Verify(ROUND(3205.618652, 1), 3205.6).Dump();
    Verify(ROUND(3205.618652, 0), 3206).Dump();
    Verify(ROUND(3205.618652, -1), 3210).Dump();
    Verify(ROUND(3205.618652, -2), 3200).Dump();

    Verify(ROUND(-3206.618652, 2), -3206.62).Dump();
    Verify(ROUND(-3206.618652, 1), -3206.6).Dump();
    Verify(ROUND(-3206.618652, 0), -3207).Dump();
    Verify(ROUND(-3206.618652, -1), -3210).Dump();
    Verify(ROUND(-3206.618652, -2), -3200).Dump();

    Verify(ROUND(-3207.618652, 2), -3206.62).Dump();
    Verify(ROUND(-3207.618652, 1), -3206.6).Dump();
    Verify(ROUND(-3207.618652, 0), -3207).Dump();
    Verify(ROUND(-3207.618652, -1), -3210).Dump();
    Verify(ROUND(-3207.618652, -2), -3200).Dump();

    Verify(ROUND(-3205.618652, 2), -3205.62).Dump();
    Verify(ROUND(-3205.618652, 1), -3205.6).Dump();
    Verify(ROUND(-3205.618652, 0), -3206).Dump();
    Verify(ROUND(-3205.618652, -1), -3210).Dump();
    Verify(ROUND(-3205.618652, -2), -3200).Dump();

    Verify(ROUND(3205.4, 0), 3204).Dump();
    Verify(ROUND(3205.6, 0), 3205).Dump();

    Verify(ROUND(-4.4, 0), -4).Dump();
    Verify(ROUND(-4.5, 0), -5).Dump();
    Verify(ROUND(-4.6, 0), -5).Dump();
    Verify(ROUND(4.4, 0), 4).Dump();
    Verify(ROUND(4.5, 0), 5).Dump();
    Verify(ROUND(4.6, 0), 5).Dump();
}

public static string Verify(double value, double expected)
{
    if (Math.Abs(value - expected) < 1e-8)
        return string.Empty;

    return value + " is not equal (enough) to " + expected;
}

public static double ROUND(double value, int decimals)
{
    if (decimals < 0)
    {
        var factor = Math.Pow(10, -decimals);
        return ROUND(value / factor, 0) * factor;
    }
    return Math.Round(value, decimals, MidpointRounding.AwayFromZero);
}
answered Sep 26, 2022 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

Excel Function Goal Seek Equivalent Algorithm in C #

Nuget has libraries available like Trident.GoalSeek and ...READ MORE

answered Oct 17, 2022 in Others by narikkadan
• 63,600 points
1,694 views
0 votes
1 answer

Excel trim function is removing spaces in middle of text - this was unexpected (?)

Create a UDF that uses VBA's version ...READ MORE

answered Oct 18, 2022 in Others by narikkadan
• 63,600 points
881 views
0 votes
1 answer

Creating a function in excel VBA to calculate the average point in a circular set of numbers

I used the following code to determine ...READ MORE

answered Oct 28, 2022 in Others by narikkadan
• 63,600 points
1,157 views
0 votes
1 answer

MAX function in Excel: is it possible to provide the range by means of variables?

Try this: =MAX(INDEX(A:A,B2):INDEX(A:A,B3)) READ MORE

answered Nov 15, 2022 in Others by narikkadan
• 63,600 points
618 views
0 votes
1 answer

Generate a flat list of all excel cell formulas

Hello, you'll have to follow certain steps ...READ MORE

answered Feb 18, 2022 in Others by gaurav
• 23,260 points
564 views
0 votes
1 answer

Convert a number to a letter in C# for use in Microsoft Excel [duplicate]

If you are familiar with using formulas ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
860 views
0 votes
1 answer

Deleting duplicate rows in Excel using Epplus

You need to re-think this… the while ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
1,514 views
0 votes
1 answer

Export DataTable to Excel File

Add Interop References. First we need to ...READ MORE

answered Jun 9, 2022 in JQuery by gaurav
• 23,260 points
783 views
0 votes
1 answer

IF function in combination with an Round function Excel

I'm not sure if it is a ...READ MORE

answered Sep 25, 2022 in Others by narikkadan
• 63,600 points
3,615 views
0 votes
1 answer

Way to overcome Excel Vlookup function limit of 256 characters

If you are using VLOOKUP like this: =VLOOKUP(A2,D2:Z10,3,FALSE) i.e. ...READ MORE

answered Sep 30, 2022 in Others by narikkadan
• 63,600 points
3,546 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP