The details of C # Division

Source: Internet
Author: User

Recently there was a place where the probabilities were used in the project, and the probability of calculating the probabilities would naturally use division. My side of the probability of the algorithm is this, from 0 to 10000 to get a random number, the random value divided by 10000 is the probability, but the careless attention to detail, the result is directly in addition to the following:
            = New Random ();
int= rdo. Next (0,10000);
float=/10000;

       If Intnum is 845, you should theoretically get 0.0845. But the result let me plunge glasses, regardless of how to calculate, the result is 0.0, if no matter how random words are 0.0, then lose meaning. So Baidu went, the original C # Division of precision is based on dividend to decide, here is the dividend is intnum, its data type is plastic, so this side of the result of the natural to take the whole. Since this way does not pass, it is only a different way, in short, the problem must be solved, so I changed to the following this writing:      

  Random RDO  =  new   Random (); 
float Intnum = ( float ) RDO. Next ( 0 10000 );
float result = Intnum / 10000

Ok, achieved, got the answer I want, such as Intnum is 845, then get the result is 0.0845.c# solved this problem, so I think of SQL Server will also have this situation, so I tried, as follows:

Select 845 / 10000

Query results equal to 0, it seems that SQL and C # is the same, so I also changed the way, as follows:

Select CONVERT (float,845) / 10000

Query result equals 0.0845, correct.?

The details of C # Division

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.