0

I have multiple numbers of minutes and hours. What I want to do, is add all them up together. For example: if I have the numbers: 31, 15 and 45. Those are for minutes. I then have 2, 3 and 1 which are for hours. I want to add all the numbers and get the hours and minutes.

I so far added all the minutes together (31 + 15 + 45 = 91), then I multiplied all the hours by 60 (2 * 60 = 120; 3 * 60 = 180; 1 * 60 = 60) so they get converted to minutes.

I then add the final numbers together (91 + 120 + 180 + 60 = 451).

The final step would be to divide 451 / 60 = 7.5166666...7.

My question is, being that what I did so far was correct, how do I get the final number to be rounded out to the hundredths place, so that 7.51666... will become 7.52?

I will be displaying the final number (7.52) in a UILabel.

This question doesn't answer my question because that question is asking how to round off numbers to the nearest .5. My question, is how to round off a number.

Community
  • 1
  • 1
  • Rouding of floating-point values typically only applies when *displaying* them, not when calculating them. With that, you should be more clear about how you're trying to display these numbers in your UI. – Jonathon Reinhart Mar 12 '15 at 05:53
  • So you need to convert your floating-point value to a string, which formatted the way you'd like. Then set the `UILabel`'s text to that string. This question has little to do with your actual time calculations. Searching for "objective c format floating point" should get you started. – Jonathon Reinhart Mar 12 '15 at 05:57
  • Sorry for the confusion, but my question is how can I round a number to the nearest hundredths place? –  Mar 12 '15 at 06:03
  • 1
    possible duplicate of [Rounding numbers in Objective-C](http://stackoverflow.com/questions/752817/rounding-numbers-in-objective-c) – Jonathon Reinhart Mar 12 '15 at 06:04
  • I don't think it's a duplicate. See my edits –  Mar 12 '15 at 06:07

3 Answers3

0

The general way to round numbers is to multiply them with the multiplicative inverse of the required precision, round the result to the nearest integer. And then divide that integer by the multiplicative inverse again.

You want to round to 0.01, the multiplicative inverse (1/x) of that is 100.

7.5166666 * 100 = 751.666666
round(751.6666) = 752
752 / 100 = 7,52

or in code:

double x = 7.5166666;
int roundingFactor = (int)(1 / 0.01); // round to 0.01
double rounded = round(x * roundingFactor)/(double)roundingFactor;

Or, if you just need the number for display purposes you should use NSNumberFormatter.maximumFractionDigits. Since you need a NSNumberFormatter anyway (so you get a nice localized format with the correct decimal separator) you should use that.

NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.numberStyle = NSNumberFormatterDecimalStyle;
nf.maximumFractionDigits = 2;
nf.roundingMode = NSNumberFormatterRoundHalfUp;
NSString *roundedString = [nf stringFromNumber:@(x)];
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • Does the second option (`NSNumberFormatter`) actually round the numbers off, or does it just leave make it to the hundredths place? –  Mar 12 '15 at 06:32
  • NSNumberFormatter has a `roundingMode` property which allows you to choose from [7 different modes](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNumberFormatter_Class/index.html#//apple_ref/swift/enum/NSNumberFormatterRoundingMode). You probably want `NSNumberFormatterRoundHalfUp` – Matthias Bauch Mar 12 '15 at 06:42
0

You can use NSNumberFormatter to round up number.

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode: NSNumberFormatterRoundUp];
NSString * numberString = [formatter stringFromNumber:<number>]
NSNumber * formattedNumber = [formatter numberFromString: numberString];
Savitha
  • 561
  • 6
  • 19
0

This is a more better approach converting numbers to hours and minutes.

convert all the numbers to minutes and then divide by 60 and get the integer part - that is hours. Till here you are going good.

Then Apply mod to the same number and get remaining minutes.

For ex: you have times as follow - 2hrs 31mins, 3hrs 45mins and 1hr 15mins. so your final minutes would be

(2*60 + 31) + (3*60 + 45) + (1*60 + 15) = 451

Now

(int)451 / 60 = 7 hrs
451 % 60 = 31 mins

Final time = 7hrs 31mins

Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51
  • 1
    In the example I gave (at my question,) it came out to 7 hr 52 min. Why does your answer come out to 7 hr 31 min? (I'm not saying 7 hr 52 min is correct, its not, but I would like to know how yours came out accurate.) –  Mar 12 '15 at 07:09
  • The modulo(%) gives you the remaining minutes. Modulo operator gives you the remainder. So upto 420 minutes it is 7hrs and then the left part is 31. Does this answer your question? – Burhanuddin Sunelwala Mar 12 '15 at 07:14