Round number to specified precision in Objective-C

arp000

I am trying to round a number to a specified precision using the half round up method of rounding. (i.e. 0.5 would be 1 rounding to one's precision). I tried the following of another SO question, however it fails to round properly:

//where x is number and precision is precision
int num = floor(log10(abs(x))) - precision + 1;
double temp = pow(10, num);
return floor(x / temp + 0.5) * temp;

Example: 2.55 rounds to 2.5 whereas I need it to round to 2.6

I was wondering if anyone had a better method of rounding a number to a given precision. I have already tried modifying this method without success.

Rich

Use NSDecimalNumber for greater precision. See NSRoundingMode for the different rounding modes (I think you want NSRoundPlain)

NSDecimalNumberHandler *rounder = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:precision raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];

NSDecimalNumber *number;
NSDecimaNumber *rounded = [number decimalNumberByRoundingAccordingToBehavior:rounder];

See the docs for more info on the BOOL parameters in the handler.

It might seem like a bit of an overkill for what you want but it ensures your numbers will rarely lose precision.

Change the scale argument of the rounder for more digits on the result (i.e. your precision variable).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Round number to specified precision in Objective-C

From Dev

In Visual C++ how can I round float values to a specified precision?

From Dev

Round down by a specific precision in C#

From Dev

Round down by a specific precision in C#

From Dev

Assembly - Round floating point number to .001 precision toward -∞

From Dev

How does one round a floating point number to a specified number of digits?

From Dev

How to round UP a decimal number to a specified number of digits?

From Java

Convert float to string with precision & number of decimal digits specified?

From Dev

Round numbers to hundredths place in objective c

From Dev

How can I round up to the nearest multiple of the specified number?

From Dev

Create Regular Expression for specified string in objective c

From Dev

objective c implicit conversion loses integer precision 'NSUInteger'

From Dev

Objective C: decimalNumberByMultiplyingByPowerOf10 looses precision on perfect division

From Dev

Objective C: decimalNumberByMultiplyingByPowerOf10 looses precision on perfect division

From Dev

random number to UILabel in objective C

From Dev

Obfuscating a number(in a string) Objective C

From Dev

Round a number based on a ratio in C#

From Dev

IOS/Objective-C: Code to Make Round Image in Custom Cell

From Dev

add round of 30 minutes to current time in objective c

From Dev

Round Float Value without increasing last digit Objective C iOS

From Dev

Using Rmpfr to round with precision in R

From Dev

Adding an index and specified number to an array in c++

From Dev

Finding IMEI number using Objective-C

From Dev

Converting decimal number to binary Objective-C

From Dev

Incrementing Number Property in AWS DynamoDB Objective C

From Dev

what is the integer (number) equivalent to .text in objective c?

From Dev

Objective C number keypad, custom comma button

From Dev

Objective-C : validating text field with number

From Dev

Objective C Regex Line starts with number

Related Related

  1. 1

    Round number to specified precision in Objective-C

  2. 2

    In Visual C++ how can I round float values to a specified precision?

  3. 3

    Round down by a specific precision in C#

  4. 4

    Round down by a specific precision in C#

  5. 5

    Assembly - Round floating point number to .001 precision toward -∞

  6. 6

    How does one round a floating point number to a specified number of digits?

  7. 7

    How to round UP a decimal number to a specified number of digits?

  8. 8

    Convert float to string with precision & number of decimal digits specified?

  9. 9

    Round numbers to hundredths place in objective c

  10. 10

    How can I round up to the nearest multiple of the specified number?

  11. 11

    Create Regular Expression for specified string in objective c

  12. 12

    objective c implicit conversion loses integer precision 'NSUInteger'

  13. 13

    Objective C: decimalNumberByMultiplyingByPowerOf10 looses precision on perfect division

  14. 14

    Objective C: decimalNumberByMultiplyingByPowerOf10 looses precision on perfect division

  15. 15

    random number to UILabel in objective C

  16. 16

    Obfuscating a number(in a string) Objective C

  17. 17

    Round a number based on a ratio in C#

  18. 18

    IOS/Objective-C: Code to Make Round Image in Custom Cell

  19. 19

    add round of 30 minutes to current time in objective c

  20. 20

    Round Float Value without increasing last digit Objective C iOS

  21. 21

    Using Rmpfr to round with precision in R

  22. 22

    Adding an index and specified number to an array in c++

  23. 23

    Finding IMEI number using Objective-C

  24. 24

    Converting decimal number to binary Objective-C

  25. 25

    Incrementing Number Property in AWS DynamoDB Objective C

  26. 26

    what is the integer (number) equivalent to .text in objective c?

  27. 27

    Objective C number keypad, custom comma button

  28. 28

    Objective-C : validating text field with number

  29. 29

    Objective C Regex Line starts with number

HotTag

Archive