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 629 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
• 63,700 points

0 votes
1 answer

## Excel Function Goal Seek Equivalent Algorithm in C #

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

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

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

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

0 votes
1 answer

## Generate a flat list of all excel cell formulas

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

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

0 votes
1 answer

## Deleting duplicate rows in Excel using Epplus

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

0 votes
1 answer

## Export DataTable to Excel File

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

0 votes
1 answer

## IF function in combination with an Round function Excel

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

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