What is the best way to find the digit at n position in a decimal number?
Posted
by Elijah
on Stack Overflow
See other posts from Stack Overflow
or by Elijah
Published on 2010-05-27T17:49:34Z
Indexed on
2010/05/27
19:11 UTC
Read the original article
Hit count: 223
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?
© Stack Overflow or respective owner