-5

I want to determine the next decimal after a certain decimal programmatically.

For example, I have the decimal 0.0000041

I want to programmatically generate 0.0000042 in a function.

If I ran the function again after running it with 0.0000042 the new number would be 0.0000043

If I ran the function with the decimal being 0.0000049 the new number would be 0.0000050 etc....

Is there a way to do this?

I do not know where to start or what references to look at to do something like this.

Thank you.

  • Possible duplicate of [Binary representation of a .NET Decimal](https://stackoverflow.com/questions/3801440/binary-representation-of-a-net-decimal) – toATwork Aug 28 '18 at 11:47
  • 4
    What should happen when you run it on `0.0000050`, which is equivalent to `0.000005`? How can the algorithm know if `0.0000051` is the "next" number and not `0.000006`? – Peter B Aug 28 '18 at 11:48
  • 1
    It's not clear from your question which part of the task you are having trouble with. What have you tried, and in what way did it not work? – Steven Doggart Aug 28 '18 at 11:48
  • That's why I'm asking how to do what I want @PeterB I don't know how to do it Programmatically – Jessie boely Aug 28 '18 at 11:50
  • @StevenDoggart I haven't tried anything because I don't know how to do what I'm asking for its basically getting the next number from a decimal – Jessie boely Aug 28 '18 at 11:51
  • "What should happen" is for you to clarify - and then you start writing a matching algorithm. Right now, "what should happen" has not been fully defined. – Peter B Aug 28 '18 at 11:51
  • You might want to define a precision, so that the task is "get the next decimal given this precision". – Palle Due Aug 28 '18 at 11:53
  • 3
    What have you tried, and how has what you've tried failed? Ideally, you should provide a [MCVE] of what you've tried, and include specific information on how it failed, with error messages and/or erroneous output. [SO] is not a code-writing service; the best questions are those which provide useful information so that those who answer can guide you to devising your own correct answer. See [Ask] a Good Question. – Jeff Zeitlin Aug 28 '18 at 11:53
  • Well, something as simple as `var x = 0.0000041m; x += 0.0000001m;` will do the trick. But it seems like it that's all you wanted to do, you wouldn't have needed to ask. That's why we're wondering what your actual question is. – Steven Doggart Aug 28 '18 at 11:55
  • @JeffZeitlin I'm not asking for a code writing service, I'm asking for help on how to do something like this, you could easily point me into a direction of references I could use to complete what I'm asking for. But thanks for the clarity on something I'm aware of. – Jessie boely Aug 28 '18 at 11:55
  • @StevenDoggart The decimal could change from 0.003 and I would want it to be 0.0031 – Jessie boely Aug 28 '18 at 11:56
  • And how is the program supposed to know whether .00000x should become .00000x1 or .00000y, where y=x+1? – Jeff Zeitlin Aug 28 '18 at 12:00
  • 3
    Why, though? Based on what rule? It's not clear to anyone except you why .003 should increment to .0031 and .0000041 should increment to .0000042. Why wouldn't .003 increment to .004? Or, rather, why wouldn't .0000041 increment to .00000411? – Steven Doggart Aug 28 '18 at 12:01
  • If you provide some code that represents your attempt at solving the problem on your own, and explain how it failed - as per my first comment that you snarked at - we just might be able to figure out what's going on in your head and properly defining the problem. – Jeff Zeitlin Aug 28 '18 at 12:03
  • Or, as said @StevenDoggart why wouldn't 0.003 increment to 0.0030001 ? – Cid Aug 28 '18 at 12:07
  • The problem is that although 0.005 and 0.0050 are different in terms of the number of significant figures, the data types in .NET don't have a built-in way of recording the number of S.F. So you will have to devise some way of doing that. – Andrew Morton Aug 28 '18 at 12:09
  • @Cid Yup. That too. The possibilities are endless :) Jessie, I'm sorry if it sounds like we are trying to poke fun at it. We aren't. We're legitimately trying to help. It's just that none of us understand what you're actually trying to achieve or which part of it you don't understand how to do. We're honestly trying to figure it out so we can help you. – Steven Doggart Aug 28 '18 at 12:11
  • 1
    @AndrewMorton There's no way to do this with a `double`, but there *is* a way you can determine the number of significant decimal places of a `decimal` type: Given `decimal d`, then: `int sigDecimalPlaces = decimal.GetBits(d)[3] >> 16 & 0xff;` – Matthew Watson Aug 28 '18 at 12:22
  • Downvoted due to inability to take constructive feedback and provide a clear answer to the question "What should happen when you run it on 0.0000050, which is equivalent to 0.000005? How can the algorithm know if 0.0000051 is the "next" number and not 0.000006?" – JMadelaine Aug 28 '18 at 12:49

1 Answers1

2

You seem to be looking for something like this: (Check below if you want to specify the precision manually)

static decimal GetNextDecimal(decimal input)
{
    int count = BitConverter.GetBytes(decimal.GetBits(input)[3])[2];
    return input + (1m / (decimal)Math.Pow(10, count));
}

Usage:

decimal num1 = 0.0000041m;
decimal num2 = 0.00002m;
decimal next = GetNextDecimal(num1);
decimal next2 = GetNextDecimal(num2);
Console.WriteLine(next);
Console.WriteLine(next2);

Output:

0.0000042
0.00003

I used a little help of this answer to get the number of the decimal places.


If you originally wanted to specify the precision manually, you can use the following instead:

static decimal GetNextDecimal(decimal input, int precision)
{
    return input + (1m / (decimal)Math.Pow(10, precision));
}

..which allows you to do something like this:

decimal num1 = 0.003m;
decimal next = GetNextDecimal(num1, 3);
decimal next2 = GetNextDecimal(num1, 4);
Console.WriteLine(next);
Console.WriteLine(next2);

Output:

0.004
0.0031