# What is the alternative for Math.Round(decValue, 2, MidpointRounding.AwayFromZero)

**URL:** <https://community.postman.com/t/what-is-the-alternative-for-math-round-decvalue-2-midpointrounding-awayfromzero/10254>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [January 22, 2020, 5:31am UTC](https://community.postman.com/t/what-is-the-alternative-for-math-round-decvalue-2-midpointrounding-awayfromzero/10254 "2020-01-22T05:31:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bharath\_Gaddam](https://avatars.discourse-cdn.com/v4/letter/b/ce73a5/32.png) [@Bharath\_Gaddam](https://community.postman.com/u/Bharath_Gaddam)\
**Post date:** [January 22, 2020, 5:31am UTC](https://community.postman.com/t/what-is-the-alternative-for-math-round-decvalue-2-midpointrounding-awayfromzero/10254/1 "2020-01-22T05:31:33Z")

</div>

for example in C#

```auto
using System;

public class Example
{
   public static void Main()
   {
      Single value = 16.325f;
      Console.WriteLine("Widening Conversion of {0:R} (type {1}) to {2:R} (type {3}): ", 
                        value, value.GetType().Name, (double) value, 
                        ((double) (value)).GetType().Name);
      Console.WriteLine(Math.Round(value, 2));
      Console.WriteLine(Math.Round(value, 2, MidpointRounding.AwayFromZero));
      Console.WriteLine();
      
      Decimal decValue = (decimal) value;
      Console.WriteLine("Cast of {0:R} (type {1}) to {2} (type {3}): ", 
                        value, value.GetType().Name, decValue, 
                        decValue.GetType().Name);
      Console.WriteLine(Math.Round(decValue, 2));
      Console.WriteLine(Math.Round(decValue, 2, MidpointRounding.AwayFromZero));
   }
}
// The example displays the following output:
// Widening Conversion of 16.325 (type Single) to 16.325000762939453 (type Double):
// 16.33
// 16.33
//    
// Cast of 16.325 (type Single) to 16.325 (type Decimal):
// 16.32
// 16.33

```

My test:

```auto
var jsonData = pm.response.json();

pm.test('Response body has accurate rebate value', function() {
    var expectedValue = JSON.parse(postman.getGlobalVariable('value')/100)*(16.706);
    Values = (jsonData.calculationResultList[0].totalAdjustment.value);

pm.expect(Values).to.eql(expectedValue);

Output I got:    
FAIL| AssertionError: expected -37.62 to deeply equal -37.6152296
    

So I tried,

let Rebate = Math.round(expectedValue, 2);
pm.expect(Values).to.eql(Rebate);

Output:    
FAIL| AssertionError: expected -37.62 to deeply equal -38
```

---

<div class="post-metadata">

**Author:** ![VincentD123](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vincentd123/32/1845_2.png) [@VincentD123](https://community.postman.com/u/VincentD123)\
**Post date:** [January 22, 2020, 7:34am UTC](https://community.postman.com/t/what-is-the-alternative-for-math-round-decvalue-2-midpointrounding-awayfromzero/10254/2 "2020-01-22T07:34:40Z")

</div>

could I suggest a bit of a different approach…?  
`pm.expect(Math.abs(expectedValue-value)).to.be.lt(0.01);`

---

<div class="post-metadata">

**Author:** ![Bharath\_Gaddam](https://avatars.discourse-cdn.com/v4/letter/b/ce73a5/32.png) [@Bharath\_Gaddam](https://community.postman.com/u/Bharath_Gaddam)\
**Post date:** [January 22, 2020, 11:23pm UTC](https://community.postman.com/t/what-is-the-alternative-for-math-round-decvalue-2-midpointrounding-awayfromzero/10254/3 "2020-01-22T23:23:29Z")

</div>

> [@VincentD123](#):
>
> pm.expect(Math.abs(expectedValue-value)).to.be.lt(0.01);

Hi @VincentD123,

Thank you for your solution.

It was’t exactly what i was looking for but it’s a workaround for now.

---

<div class="post-metadata">

**Author:** ![aschultz](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@aschultz](https://community.postman.com/u/aschultz)\
**Post date:** [January 29, 2020, 2:33pm UTC](https://community.postman.com/t/what-is-the-alternative-for-math-round-decvalue-2-midpointrounding-awayfromzero/10254/4 "2020-01-29T14:33:26Z")

</div>

Found this gem by Jack Moore  
[https://www.jacklmoore.com/notes/rounding-in-javascript/](https://www.jacklmoore.com/notes/rounding-in-javascript/)

```
function round(value, decimals) {
  return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}

var value = -37.6152296;

var expectedValue = round(value, 2);

pm.expect(-37.62).to.eql(expectedValue);
```

---

<div class="post-metadata">

**Author:** ![Bharath\_Gaddam](https://avatars.discourse-cdn.com/v4/letter/b/ce73a5/32.png) [@Bharath\_Gaddam](https://community.postman.com/u/Bharath_Gaddam)\
**Post date:** [January 30, 2020, 12:37am UTC](https://community.postman.com/t/what-is-the-alternative-for-math-round-decvalue-2-midpointrounding-awayfromzero/10254/5 "2020-01-30T00:37:09Z")

</div>

@aschultz,

Thank you.
