Getting Started with Adobe After Effects - Part 6: Motion Blur
A collection of quick technology learning tips from what people around you learn every day

To effectively convert a decimal to nearest integer

Jun 14 2012 12:00AM by satyajit   

Recently I needed to convert a float value to nearest integer...

I used Convert.ToInt32 The problem found with is it will convert decimal like 7.5,8.5 to nearest even integer

Convert.ToInt32(7.5)=>8 
Convert.ToInt32(8.5)=>8 !!!

The solution found initially was to use Math.Round as below

Math.Round(8.5,MidpointRounding.AwayFromZero)=>9

Now this is not so flexible...as some times We need to convert 8.5 to 8 and other times to 9

So Below is the best solution I found to round up/down

   static int convert_up(double d)
        {
            return (int)(d + 0.5d);
        }

   static int convert_down(double d)
        {
            return (int)(d + 0.4d);
        }

It is too flexible to modify.

Please let me know your thoughts

Read More..   [32134 clicks]

Published under: Microsoft .NET Tips ·  ·  ·  · 


satyajit
124 · 1% · 402
3
 
2
 
 
 
0
Interesting
 
0
Forgotten



Submit

12  Comments  

  • I think banks do this rounding of .5 to the nearest even integer because this kind of value (.5) occurs quite a lot in monetary amounts. It has the effect of distributing evenly the rounding down and rounding up occurrences.

    commented on Jun 14 2012 4:06AM
    dishdy
    17 · 10% · 3262
  • That can be one reason... But problem occurs if even-odd distribution has lot of variance say 100 7.5 and 1000 8.5 ......

    commented on Jun 14 2012 6:08AM
    satyajit
    124 · 1% · 402
  • This will be unlikely in the zillion numbers running around in banks.

    commented on Jun 14 2012 6:30AM
    dishdy
    17 · 10% · 3262
  • It is a interesting topic you stumbled upon however I am not sure your solution is the answer

    Rounding 8.55 with convert_down => 8

    Since it over .5 rounding up or down should go to 9

    you almost need a check just for this special case

    commented on Jun 14 2012 7:05AM
    mbova407
    320 · 0% · 132
  • @mbova407

    Rounding 8.55 with convert_down => 8

    What I mean rounding up is the default case.... eg. 4.4->4 ,4.8->5 This is rounding up that I meant

    4.4->4,4.8->4 ....is rounding down that I mean..

    may be my choice of words cause confusion to you...

    commented on Jun 14 2012 7:38AM
    satyajit
    124 · 1% · 402
  • In the old days of Numerical Control when many of the machines only understood incremental positioning and it was critical to loop to the exact same location in a program within .0001in, and the biggest integer you could possibly get was 34767, we relied on scaling and double precision to get it right. Out = SIGN(dIn) * DINT((ABS(dIn) * 10000.d0) + .5d0) / 10000.d0 (Yes that is FORTRAN)
    Note that the value is scaled to the precision that you want and essentially truncated after .5 is added.

    commented on Jun 14 2012 10:30AM
    Dave Vroman
    132 · 1% · 375
  • Removing the .8 from 4.8 to yield 4 is not "rounding", it is truncation.

    Additionally the advice to add 0.4 is wrong and dangerous. It must not be considered a valid algorithm for dealing with rounding and truncation. All readers, please do not listen to this incorrect advice.

    More rigorous logic and math need to be applied to problems of this sort than apparent but deceptively wrong "quick fixes" such as adding 0.4.

    commented on Jun 17 2012 10:48PM
    ErikEckhardt
    65 · 3% · 887
  • Well, satyajit's technique will work if you are only dealing with numbers with exactly one digit after the decimal point. Thus it is a very limited approach.

    I see only three real world needs for converting doubles to integers:

    • the classic one which you get with (int)(mynumber+0.5d)
    • the bank rounding obtained with Convert.ToInt32(mynumber)
    • truncating obtained by just casting to an integer

    I have never encountered the need for me to decide whether 8.5 should go up or down, i.e. different from the bank rounding. Maybe satyajit should explain to us exactly what kind of rounding he is doing, i.e. the reason for why it needs to be different from the above three.

    commented on Jun 18 2012 5:33AM
    dishdy
    17 · 10% · 3262
  • Hi..Sorry for late reply ...Let me tell you where I needed this type of approach...

    I am working on a plan management tool where user has to give their estimate.. user can give estimate like 8.4,8.6 etc... It means 8 days and .4 of another day....

    Now when we generate a report client needed x.4=>x , x.6=>x+1 , x.5=>x+1

    So I tried conver.toint32 ...but as I told it has its problem which didnt suit my set of decimals

    So we used Math.Round(8.5,MidpointRounding.AwayFromZero)

    now (int)(mynumber+0.5d) is another way to do the same ....

    while doing this I found this way to be very flexible...

    Say tommorow my client say they need x.4=>x , x.6=>x+1 , x.5=>x which I think is not unlikely

    Please let me know how will you do it if you are aware of some other better approach ?

    Just writing that its just a quick fix will not help me to learn(for which I am in this forum :) )

    Also @dishdy Well, satyajit's technique will work if you are only dealing with numbers with exactly one digit after the decimal point. Thus it is a very limited approach.

    8.566 => 9 ... using (int)(mynumber+0.5d)

    8.566 => 8 ... using (int)(mynumber+0.4d)

    is it wrong ? Please let me know ...

    commented on Jun 19 2012 1:38AM
    satyajit
    124 · 1% · 402
  • satyajit,

    So you are confirming that you are using this for numbers having only one digit after the decimal point.

    Yet you are saying it is also valid for numbers like 8.566. I have never encountered a problem where all numbers in the range X.0 to X.5999999... should be rounded down to X and all numbers in the range X.6 to X.999999... should be rounded up to X+1.

    commented on Jun 19 2012 1:59AM
    dishdy
    17 · 10% · 3262
  • @dishdy Yes ...in my project upyo 1 digit....

    What I meant was it will work(8.566...) isnt it?

    I am not saying that this will/will not occur in a real situation....(never mentioned too...)..

    Thanks for your interest...

    commented on Jun 19 2012 2:52AM
    satyajit
    124 · 1% · 402
  • @eric

    I want to know the correct way....Just if you write this is wrong isnt helping any 1

    commented on Jun 19 2012 3:00AM
    satyajit
    124 · 1% · 402

Your Comment


Sign Up or Login to post a comment.

"To effectively convert a decimal to nearest integer" rated 5 out of 5 by 3 readers
To effectively convert a decimal to nearest integer , 5.0 out of 5 based on 3 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]