How to convert 2,22292E+12 to 2222921601281

Asked

Viewed 39 times

2

I have a method that returns a float (but I can change this return to the other if I need to)

 float mmc(int a, int b)
    {
        float num1, num2, x, y, mmc = 0;
        
        num1 = a;
        num2 =b;
        x = num1;
        y = num2;
        while (num1 != num2)
        {
            if (num1 > num2)
            {
                num1 = num1 - num2;
            }
            else
            {
                num2 = num2 - num1;
            }
        }

        float resultado =(x * y) / num1;
        mmc = (x * y) / num1;
        return mmc;
    }

When passing high values (which will always occur) it returns letters and numbers. The formula is right, in Excel is beating, but there I format the cell to number and then I get the correct result.
example mmc returned result = 2.22292E+12 and how I need 2222921601281.
How do I convert or treat this value to c# ?

1 answer

3


You can use decimal instead of float to obtain a more precise result. Floats and doubles work with rounding values, so they wouldn’t be a viable option to calculate mmc, because you need an exact number, not a rounded one.

Ex: MMC(889787, 977541)

Output em float: 8,698034E+12

Output em decimal: 8698033627457

The function would look like this:

decimal mmc(int a, int b)
{
    decimal num1, num2, x, y, mmc = 0;

    num1 = a;
    num2 = b;
    x = num1;
    y = num2;
    while (num1 != num2)
    {
        if (num1 > num2)
        {
            num1 = num1 - num2;
        }
        else
        {
            num2 = num2 - num1;
        }
    }

    decimal resultado = (x * y) / num1;
    mmc = (x * y) / num1;
    return mmc;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.