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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:25:00+00:00 2026-05-15T02:25:00+00:00

Background I’m working on a symmetric rounding class and I find that I’m stuck

  • 0

Background

I’m working on a symmetric rounding class and I find that I’m stuck with regards to how to best find the number at position x that I will be rounding. I’m sure there is an efficient mathematical way to find the single digit and return it without having to resort to string parsing.

Problem

Suppose, I have the following (C#) psuedo-code:

var position = 3;
var value = 102.43587m;
// I want this no ↑ (that is 5)

protected static int FindNDigit(decimal value, int position)
{
    // This snippet is what I am searching for
}

Also, it is worth noting that if my value is a whole number, I will need to return a zero for the result of FindNDigit.

Does anyone have any hints on how I should approach this problem? Is this something that is blaringly obvious that I’m missing?

  • 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-15T02:25:01+00:00Added an answer on May 15, 2026 at 2:25 am
    using System;
    
    public static class DecimalExtensions
    {
        public static int DigitAtPosition(this decimal number, int position)
        {
            if (position <= 0)
            {
                throw new ArgumentException("Position must be positive.");
            }
    
            if (number < 0)
            {
                number = Math.Abs(number);
            }
    
            number -= Math.Floor(number);
    
            if (number == 0)
            {
                return 0;
            }
    
            if (position == 1)
            {
                return (int)(number * 10);
            }
    
            return (number * 10).DigitAtPosition(position - 1);
        }
    }
    

    Edit:
    If you wish, you may separate the recursive call from the initial call, to remove the initial conditional checks during recursion:

    using System;
    
    public static class DecimalExtensions
    {
        public static int DigitAtPosition(this decimal number, int position)
        {
            if (position <= 0)
            {
                throw new ArgumentException("Position must be positive.");
            }
    
            if (number < 0)
            {
                number = Math.Abs(number);
            }
    
            return number.digitAtPosition(position);
        }
    
        static int digitAtPosition(this decimal sanitizedNumber, int validPosition)
        {
            sanitizedNumber -= Math.Floor(sanitizedNumber);
    
            if (sanitizedNumber == 0)
            {
                return 0;
            }
    
            if (validPosition == 1)
            {
                return (int)(sanitizedNumber * 10);
            }
    
            return (sanitizedNumber * 10).digitAtPosition(validPosition - 1);
        }
    

    Here’s a few tests:

    using System;
    using Xunit;
    
    public class DecimalExtensionsTests
    {
                             // digit positions
                             // 1234567890123456789012345678
        const decimal number = .3216879846541681986310378765m;
    
        [Fact]
        public void Throws_ArgumentException_if_position_is_zero()
        {
            Assert.Throws<ArgumentException>(() => number.DigitAtPosition(0));
        }
    
        [Fact]
        public void Throws_ArgumentException_if_position_is_negative()
        {
            Assert.Throws<ArgumentException>(() => number.DigitAtPosition(-5));
        }
    
        [Fact]
        public void Works_for_1st_digit()
        {
            Assert.Equal(3, number.DigitAtPosition(1));
        }
    
        [Fact]
        public void Works_for_28th_digit()
        {
            Assert.Equal(5, number.DigitAtPosition(28));
        }
    
        [Fact]
        public void Works_for_negative_decimals()
        {
            const decimal negativeNumber = -number;
            Assert.Equal(5, negativeNumber.DigitAtPosition(28));
        }
    
        [Fact]
        public void Returns_zero_for_whole_numbers()
        {
            const decimal wholeNumber = decimal.MaxValue;
            Assert.Equal(0, wholeNumber.DigitAtPosition(1));
        }
    
        [Fact]
        public void Returns_zero_if_position_is_greater_than_the_number_of_decimal_digits()
        {
            Assert.Equal(0, number.DigitAtPosition(29));
        }
    
        [Fact]
        public void Does_not_throw_if_number_is_max_decimal_value()
        {
            Assert.DoesNotThrow(() => decimal.MaxValue.DigitAtPosition(1));
        }
    
        [Fact]
        public void Does_not_throw_if_number_is_min_decimal_value()
        {
            Assert.DoesNotThrow(() => decimal.MinValue.DigitAtPosition(1));
        }
    
        [Fact]
        public void Does_not_throw_if_position_is_max_integer_value()
        {
            Assert.DoesNotThrow(() => number.DigitAtPosition(int.MaxValue));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I'm writing an app that uses the ViewPager and that's working nicely. I
Background: We're building an application that allows our customers to supply data in a
Background: I would like to dismiss a modalView that I have presented earlier and
Background I am working with a monad built of a stack of transformers one
Background: I have a css and a js that is used only by the
Background: My employeer at my non-programming job knows that I am an undergraduate CS
Background This is only my second PyQt4 project. Developing a Windows app that has
Background I have a basic HTML page with an iframe that points to a
Background My project is urgent and requires that I iterate a large XML file
Background: I'm working through the Programming Collective Intelligence book by Toby Segaran; specifically the

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.