Decimal to int

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sasi Rekha
    New Member
    • Aug 2007
    • 18

    Decimal to int

    Hi is there any function in c#(other than ceiling and floor functions) which returns output as follows.

    if i provide an input of 1.4 it should return 1
    if i provide an input of 1.6 it should return 2

    Please reply back ASAP.
    Thanks
    Sasi
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Have you tried Math.Round

    Comment

    • Sasi Rekha
      New Member
      • Aug 2007
      • 18

      #3
      Originally posted by Shashi Sadasivan
      Have you tried Math.Round
      ya i tried it.. but no use :(

      its result is 1.4 ->1 and 1.6 ->1

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        I dont know how you have implemented the Math.Round Method

        try this
        [CODE=cpp]double p = 1.4, q = 1.6;
        int i = Convert.ToInt32 (Math.Round(p, 0));
        int j = Convert.ToInt32 (Math.Round(q, 0));
        Console.WriteLi ne("i = {0}, j={1}", i, j);[/CODE]

        works good for me

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          A little trick I learned in Fortran:
          Just add 0.5 and cast it to an Integer.
          The integer will implicitly floor(), so adding 0.5 first will correctly round for you.

          Comment

          Working...