Mathematical rounding

via Digitalmars-d digitalmars-d at puremagic.com
Thu Oct 16 00:31:13 PDT 2014


On Thursday, 16 October 2014 at 05:24:23 UTC, Andrei Alexandrescu 
wrote:
> On 10/15/14, 8:11 PM, Elena wrote:
>> Hi,every one!
>> How can I realize mathematical rounding (not banking) for 
>> Double in D
>> 0.4 -> 0
>> 0.5 -> 1
>> 1.5 -> 2
>> 2.5 -> 3
>> 3.5 -> 4  ?
>>
>> Thank you.
>
> Add 0.5 and then cast to integral. -- Andrei

This would be the "round half up" mode that produce 0 for -0.5:

floor(x+0.5)

Whereas "round nearest" should produce -1 for -0.5:

floor(abs(x)+0.5)*sgn(x)


More information about the Digitalmars-d mailing list