Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4233186
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:11:19+00:00 2026-05-21T02:11:19+00:00

I have the following code in vb.net that calculates the amount before tax was

  • 0

I have the following code in vb.net that calculates the amount before tax was applied:

Public Shared Function CalculateRateBeforeTax(ByVal rate As Decimal, ByVal tax As Decimal) As Decimal
    Dim base As Decimal = rate / (1 + (tax / 100.0))
    Return Math.Round(base,2)
End Function

Some scenarios I setup were:

Rate = 107, Tax = 7%, Base = 100

Rate = 325, Tax = 6.5%, Base = 305.16

Rate = 215, Tax = 125%, Base = 95.55

I put the above scenarios into some unit tests using c# and using the nunit testing framework. The first scenario passes, but the other fails and I am not sure how I can get it to pass. Here is are my tests:

[TestFixture]
class TaxTests
{
    [Test]
    public void CalculateRateBeforeTax_ShouldReturn100_WhenRateIs107AndTaxIs7Percent()
    {
        decimal expected = 100.0m;
        decimal actual = TaxUtil.CalculateRateBeforeTax(107.0m, 7.0m);

        Assert.AreEqual(expected,actual);
    }

    [Test]
    public void CalculateRateBeforeTax_ShouldReturn305point16_WhenRateIs325AndTaxIs6point5Percent()
    {
        decimal expected = 305.16m;
        decimal actual = TaxUtil.CalculateRateBeforeTax(325.0m, 6.5m);

        Assert.AreEqual(expected, actual);
    }

    [Test]
    public void CalculateRateBeforeTax_ShouldReturn95point55_WhenRateIs215AndTaxIs125Percent()
    {
        decimal expected = 95.55m;
        decimal actual = TaxUtil.CalculateRateBeforeTax(215.0m, 125.0m);

        Assert.AreEqual(expected, actual);
    }

}

As I said before, the first test passes, but the results of the other tests are:

Second Test expected 305.1600000000000003d But was: 305.1643192488263d

Third Test expected 95.54999999999997 But was: 95.55555555555555557d

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:11:20+00:00Added an answer on May 21, 2026 at 2:11 am

    Just take out a calculator and enter the following: 325 / (1 + (6.5 / 100.0))

    The result is 305.164319…

    Then you’re asking if 305.164319… is equal to 305.16. The test obviously fails, they are not the same numbers.

    Now if you’re wondering why you have slightly different numbers than this, like 305.1600000000000003 instead of 305.16, this is because there is some loss of precision with Double type. You can use the Decimal type for more precision.

    But the most important problem is that the value returned by CalculateRateBeforeTax is not truncated correctly to have precision up to the cent. You just have to truncate two decimals like this:

    Dim rounded As Decimal = Math.Floor(base * 100) / 100
    

    Now by changing Double type by Decimal type your Assert should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code that I am compiling in a .NET 4.0 project namespace
I have the following code that is working fine in jsfiddle - http://jsfiddle.net/darkajax/FHZBy/ I've
I have the following code in my ASP.NET project public sealed class IoC {
I have the following code in a full .NET framework solution: public delegate int
So I have the following VB.NET code that creates a form field in a
I have the following code in C# and .net 3.5 that is working fine
I have the following code: import java.net.InetAddress; public class lookup { public static void
I have the following code that adds a background worker into a VB.net WPF
I have an ASP.NET program that sends a confirmation email with the following code:
I have the following code - http://jsfiddle.net/vcvsb/1/ - per $(document).ready(function(e) { $(input#autocomplete).autocomplete({ source: [c++,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.