WAQ
WAQ

Reputation: 2626

Create a double value from two separate variables C#

I have two seperate variables x and y of Integer types

Lets say x = 123 and y = 456. I want to create a double using these two variables such that result = 123.456.

How do i get this?

Upvotes: 2

Views: 632

Answers (1)

user2160375
user2160375

Reputation:

public static double Combine(int x, int y)
{
    if (x < 0 || y < 0) throw new NotSupportedException(); // need to specify
           // how it should behave when x or y is below 0
    if (y == 0) return x;

    var fractionMultipler = (int)Math.Floor(Math.Log10(y)) + 1;
    var divider = Math.Pow(10, fractionMultipler);


    return x + (y / divider);
}

Sample:

 var z = Combine(30, 11123); // result 30.11123

Upvotes: 3

Related Questions