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 in Others by Kithuzzz
• 20,660 points
54 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 by narikkadan
• 37,660 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 in Others by narikkadan
• 37,660 points
80 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 in Others by narikkadan
• 37,660 points
44 views
0 votes
1 answer
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 in Others by narikkadan
• 37,660 points
24 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 in Others by gaurav
• 22,040 points
76 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 in Database by gaurav
• 22,040 points
141 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 in Database by gaurav
• 22,040 points
287 views
0 votes
1 answer

Export DataTable to Excel File

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

answered Jun 9 in JQuery by gaurav
• 22,040 points
105 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 in Others by narikkadan
• 37,660 points
58 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 in Others by narikkadan
• 37,660 points
127 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