-3
    static void Main()
    {
       var result1 = Converter.ConvertToInt32("922336666");
       //result1 one return as 922336666
       var result2 = Converter.ConvertToInt64("92233720368547758");
       //result2 one return as 92233720368547760
    }
}
public static class Converter {
    public static int ConvertToInt32(string input) {
        return Convert.ToInt32(Math.Round(Convert.ToDouble(input)));
    }
    public static Int64 ConvertToInt64(string input)
    {
        return Convert.ToInt64(Math.Round(Convert.ToDouble(input)));
    }
}

I am getting result2 rounded up to 92233720368547760 but result1 is kept the same. Can some one explain. When using ToDouble does round up the int ?

Mohamed
  • 55
  • 9
  • Why are you converting the string to a double, rounding and then to an int64? Try `Convert.ToInt64(input)`. You're losing precision by going via double! – phuzi Nov 06 '22 at 15:59
  • @phuzi the reason why is that let’s say I have a string this string could be like “10.5” or could be like “10” then I will need to convert to double then I am able to round then convert to in but even just converting to double without rounding up results in rounding the last two digits – Mohamed Nov 06 '22 at 16:07
  • 2
    if you _do_ need to go that way, simply use a data type appropriate for the use case. meaning `Decimal`. or: just cut off the `.5`-part of your string. but either way: the inner workings of floating point numbers, and their inherent limitations, is something every programmer ought to know and understand. the TL;DR: don't ever expect them to be _precise_ – Franz Gleichmann Nov 06 '22 at 16:31
  • @FranzGleichmann yes I tried with decimal before and did work my understanding that to double should convert to double with .00. Not sure why in my case is rounding the last digits when the int of type 64 also not sure what you mean by .5 – Mohamed Nov 06 '22 at 16:39
  • They say they would like stackoverflow to be good place for developers what kind of developers do you expect all need to be intelligent smart to use the service I can do good work just came across something that I don’t understand. If it’s paid service then all will contribute I hope not because even if it’s going to be paid so everybody has right to ask it’s always good to be for certain group that I do not see a difference when I am among them just some time we did not had opportunity like them to learn everything back in time may a lot will not agree that I can write apps but thnx god I can – Mohamed Nov 06 '22 at 16:49
  • Note that numbers are stored as `x = ±( 1 + m/2^52) * 2^(e-1023)` where `m` is a 52-bit integer (`0..4503599627370495`) and `e` is a 11-bit integer (`0..2047`). – John Alexiou Nov 06 '22 at 17:13
  • 1
    @Mohamed, please do not take this personally. Users on SO up- or down-vote questions and answers, not persons. Your question was probably downvoted because some of the users consider it as a duplicate. You are always welcome on this site. – Olivier Jacot-Descombes Nov 06 '22 at 17:41
  • @OlivierJacot-Descombes sure will do – Mohamed Nov 06 '22 at 18:13

1 Answers1

4

The documentation of System.Double Struct (double in C#) says:

A Double value has up to 15 decimal digits of precision

Where about 17 digits are stored but the extra digits are not guaranteed to be precise. Note that the number is effectively stored in a binary format, so the number of decimal digits is only an approximation.

Your second number has 17 digits. This explains why the conversion was not accurate.

Use a System.Decimal Struct (decimal in C#) instead. According to Floating-point numeric types (C# reference) it has a precision of 28-29 digits.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • 1
    Great answer of you this should keep me going with this for now will read the documentation just needed some guide thanks Mr Olivier Jacot Descombes – Mohamed Nov 06 '22 at 16:54