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 6747433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:26:29+00:00 2026-05-26T12:26:29+00:00

Can someone please provide code (any language will do, but I write the .Net

  • 0

Can someone please provide code (any language will do, but I write the .Net languages and VB6) for an algorithm that rounds numbers ending with 5 towards a given number?

RoundTo(double value, double toWards, int numberOfDigitsBehindComma)

RoundTo(1.25,1,1)=1.2
RoundTo(1.25,2,1)=1.3

RoundTo(1.26,1,1)=1.3
RoundTo(1.24,2,1)=1.2

Include a solution for negative numbers please.

EDIT: There seems to be a lot of confusion about my requirements I will fill in all the Assertions that the resulting code has to meet. My solution does so.

  [TestMethod]
   public void RoundTowards()
   {
      double x=3.44;double y=3.45;double z=4.45;
      double a = 3.51; double b = 4.5001; double c = -1.14; double d = -1.15;
      var mean=4;
      Assert.AreEqual(3.4,x.RoundTowards(mean,1));
      Assert.AreEqual(3.5, y.RoundTowards(mean, 1));
      Assert.AreEqual(4.4, z.RoundTowards(mean, 1));
      Assert.AreEqual(3.5, a.RoundTowards(mean, 1));
      Assert.AreEqual(4.5, b.RoundTowards(mean, 1));
      mean = 5;
      Assert.AreEqual(3.4, x.RoundTowards(mean, 1));
      Assert.AreEqual(3.5, y.RoundTowards(mean, 1));
      Assert.AreEqual(4.5, z.RoundTowards(mean, 1));
      Assert.AreEqual(3.5, a.RoundTowards(mean, 1));
      Assert.AreEqual(4.5, b.RoundTowards(mean, 1));
      mean = 3;
      Assert.AreEqual(3.4, x.RoundTowards(mean, 1));
      Assert.AreEqual(3.4, y.RoundTowards(mean, 1));
      Assert.AreEqual(4.4, z.RoundTowards(mean, 1));
      Assert.AreEqual(3.5, a.RoundTowards(mean, 1));
      Assert.AreEqual(4.5, b.RoundTowards(mean, 1));
      Assert.AreEqual(Math.Round(-1.1,4),Math.Round( c.RoundTowards(mean, 1),4));
      Assert.AreEqual(Math.Round(-1.1,4),Math.Round(d.RoundTowards(mean, 1),4));
      mean = -2;
      Assert.AreEqual(Math.Round(3.4,4),Math.Round( x.RoundTowards(mean, 1),4));
      Assert.AreEqual(Math.Round(3.4,4),Math.Round( y.RoundTowards(mean, 1),4));
      Assert.AreEqual(Math.Round(4.4,4),Math.Round( z.RoundTowards(mean, 1),4));
      Assert.AreEqual(Math.Round(3.5,4),Math.Round( a.RoundTowards(mean, 1),4));
      Assert.AreEqual(Math.Round(4.5,4),Math.Round( b.RoundTowards(mean, 1),4));
      Assert.AreEqual(Math.Round(-1.1, 4), Math.Round(c.RoundTowards(mean, 1), 4));
      Assert.AreEqual(Math.Round(-1.2, 4), Math.Round(d.RoundTowards(mean, 1), 4));

   }

    [TestMethod]
    public void RoundTowardsTowardZero()
    {
        double x = 3.45; double y = -3.45;
        double a = -3.551; double b = 4.551; double c = 4.5500001; double d = 4.5501;
        var mean = 0;
        Assert.AreEqual(3.4, x.RoundTowards(mean, 1));
        Assert.AreEqual(-3.4, y.RoundTowards(mean, 1));
        Assert.AreEqual(-3.6, a.RoundTowards(mean, 1));
        Assert.AreEqual(4.6, b.RoundTowards(mean, 1));
        Assert.AreEqual(4.5, c.RoundTowards(mean, 1));
        Assert.AreEqual(4.6, d.RoundTowards(mean, 1));

    }

  [TestMethod]
  public void Test14_55()
  {
      Assert.AreEqual((14.55).RoundTowards(9, 1) ,14.5);
      Assert.AreEqual((14.55).RoundTowards(15,1), 14.6);
  }

  [TestMethod]
  public void Test14_5499999()
  {
      Assert.AreEqual((14.54999999).RoundTowards(9, 1) ,14.5);
      Assert.AreEqual((14.54999999).RoundTowards(15,1), 14.6);
  }

Thanks!!!

  • 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-26T12:26:30+00:00Added an answer on May 26, 2026 at 12:26 pm

    I think all the solutions here are too complicated. Your problem seems to be that you want to be able to control the direction of rounding when you are exactly at the midpoint. You can reduce the problem of having N digits trailing the decimal point just by multiplying and then dividing by an integer power of 10, so it’s enough to fix this for the case where the “5” is right after to the decimal point. If you want to round number x so that e.g. 0.5 is rounded upwards to 1, you just do

    result = floor(x + 0.5);
    

    If you want to round x so that 0.5 is rounded downwards to 0, you just do

    result = ceil(x - 0.5);
    

    These work because if x = 0.5, floor(x + 0.5) = floor(1) = 1, and ceil(x – 0.5) = ceil(0) = 0. To see that other numbers are rounded always correctly,

    x = 0.4: floor(x + 0.5) = floor(0.9) = 0
             ceil(x - 0.5) = ceil(-0.1) = 0
    x = 0.6: floor(x + 0.5) = floor(1.1) = 1
             ceil(x - 0.5) = ceil(0.1) = 1
    

    so the whole code becomes:

    double RoundTo(double value, double towards, int digits) {
      double mult = pow(10, digits); /* to handle variable number of digits */
      bool downwards = (towards < value);
      value *= mult; /* scale */
      value = (downwards ? ceil(value - 0.5)    /* round midpoint downwards */
                         : floor(value + 0.5)); /* round midpoint upwards   */
      return value / mult; /* scale back */
    }
    

    This solution also offloads the whole process to the actual mathematics library and your CPU’s ALU, and is therefore very robust. This handles obviously negative numbers without any extra tweaking, and works correctly with infinities etc.

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

Sidebar

Related Questions

Can someone please provide an example of creating a Java ArrayList and HashMap on
Can someone please point me to articles or books that discusses different programming paradigm
Can someone please derive a concrete example from the following: http://www.urdalen.com/blog/?p=210 ..that shows how
Can someone provide me with an optimized .htaccess configuration that handles compression, browser caching,
can someone please provide an example of how to use uBLAS product to multiply
Can someone please point me to the easiest way to have a timer in
Can someone please let me know how to get the different segments of the
Can someone please explain me what's the difference between Swing and AWT? Are there
Can someone please explain why int (0.4 * 10.0) is 4 yet int ((2.4
Can someone please help me out with printing the contents of an IFrame via

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.