About Erlang's rounded up and rounded down

Source: Internet
Author: User

In the Erlang API, Erlang: trunc/1 is nearest integer, and Erlang: Round/1 is rounding,

Sort it out: positive numbers are rounded up and down,

1% % rounded up to 2 Ceil (N)-> 3 T = trunc (N), 4 case n = T of5 true-> T; 6 false-> 1 + T7 end.
1% % rounded down to 2 floor (x)-> 3 T = trunc (x), 4 case (x <t) of5 true-> T-1; 6 _-> T7 end.

 

The rounded up and down of negative numbers can be used in all numbers (including positive numbers and negative numbers) with a slight flexibility ):

% Is the smallest integer Ceil (x)-> T = trunc (x), if X-T = 0-> T; true-> If x> 0-> T + 1; true-> T end.

 

% Integer floor (x)-> T = trunc (x), if X-T = 0-> T; true-> If x> 0-> T; true-> T-1 end.

 

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.