Domed Sticker

Domed Sticker Online


A private, secure home for your teams questions and answers.

Sign uporlog into customize your list.

Start here for a quick overview of the site

Detailed answers to any questions you might have

Discuss the workings and policies of this site

Learn more about Stack Overflow the company

Learn more about hiring developers or posting ads with us

This site uses cookies to deliver our services and to show you relevant ads and job listings. By using our site, you acknowledge that you have read and understand ourCookie PolicyPrivacy Policy, and ourTerms of Service. Your use of Stack Overflows Products and Services, including the Stack Overflow Network, is subject to these policies and terms.

I have a double value as 22.368511 I want to round it to 2 decimal places. i.e. it should return 22.37

Edit: I also got this if your concerned about the display of the point in cases of 25.00

NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init]; [fmt setPositiveFormat:@0.]; NSLog(@%@, [fmt stringFromNumber:[NSNumber numberWithFloat:25.342]]); NSLog(@%@, [fmt stringFromNumber:[NSNumber numberWithFloat:25.3]]); NSLog(@%@, [fmt stringFromNumber:[NSNumber numberWithFloat:25.0]]);

2010-08-22 15:04:10.614 a.out[6954:903] 25.34 2010-08-22 15:04:10.616 a.out[6954:903] 25.3 2010-08-22 15:04:10.617 a.out[6954:903] 25

method did not exist (defined only for Mac), but still – great answer, saved me hours of experimenting, so +1 and big thanks to you 🙂

One problem: is doesnt round, but truncates. Presumably, using the input value provided by the questioner with your code, the result would not be what he expected (22.37)rather (22.36).

But you can not use it for further calculation

You can use the below code to format it to two decimal places

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterDecimalStyle]; [formatter setMaximumFractionDigits:2]; [formatter setRoundingMode: NSNumberFormatterRoundUp]; NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:22.368511]]; NSLog(@Result…%@,numberString);//Result 22.37

let formatter = NumberFormatter() formatter.numberStyle = .decimal formatter.maximumFractionDigits = 2 formatter.roundingMode = .up let str = String(describing: formatter.string(from: 12.2345)!) print(str)

This is the correct (and quite impressive and thorough) answer, which should have been awarded recognition over the latter.

In swift 4 : let str = String(describing: formatter.string(from: 12.2345)!)

Is it possible that do not increase 6 to 7 in 22.36 value of result?

value = (round(value*100)) / 100.0;

I believe this is the most correct answer – he asked to just round it – not to convert it into a string at the end (or do a double conversion back to double afterwards).

[label setText:@Value: %.2f, myNumber];

You can use theNSDecimalRoundfunction

that is useful if your rounding the value, (which I also thought was the question at first) but the comments show that hmthur wats to control the display only …

To remove the decimals from your double, take a look at this output

let hellodouble:Double = 3.979 print(String(format:Your value with 2 decimals: %.2f, hellodouble)) print(String(format:Your value with no decimals: %.0f, hellodouble))

I was going to go with Jasons answer but I noticed that in My version of Xcode (4.3.3) I couldnt do that. so after a bit of research I found they had recently changed the class methods and removed all the old ones. so heres how I had to do it:

NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init]; [fmt setMaximumFractionDigits:2]; NSLog(@%@, [fmt stringFromNumber:[NSNumber numberWithFloat:25.342]]);

Use NSNumber *aNumber = [NSNumber numberWithDouble:number]; instead of NSNumber *aNumber = [NSNumber numberWithFloat:number];

+(NSString *)roundToNearestValue:(double)number NSNumber *aNumber = [NSNumber numberWithDouble:number]; NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; [numberFormatter setUsesGroupingSeparator:NO]; [numberFormatter setMaximumFractionDigits:2]; [numberFormatter setMinimumFractionDigits:0]; NSString *string = [numberFormatter stringFromNumber:aNumber]; return string;

For Swift there is a simple solution if you cant either import Foundation, use round() and/or does not want a String (usually the case when youre in Playground):

NSNumberFormatter* f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle]; [f setFormat:@0.00]; // test NSNumber* a = @12; NSString* s = [f stringFromNumber:a]; NSLog(@%@, s);

By clicking Post Your Answer, you acknowledge that you have read our updatedterms of serviceprivacy policyandcookie policy, and that your continued use of the website is subject to these policies.

How Stack Overflow for Teams Fits into the Community

Rent cheque was stolen from landlord – am I obligated to pay again?

How to Differentiate Between Demonic Possession and Mental Illness

Can this helicopter/wrecking ball combo remain airborne?

Why does having a soda tax seem so hard to achieve?

When creating an empty file, why might one prefer touch file over : file?

Can you take different archetype options at different levels?

Why are research papers written in language thats difficult for undergraduate students?

Are there any reliable plants to use for navigation?

Benefit of using Vector2 data type over separate x & y variables

Is a professor allowed to require me to give him permission to speak with my doctor before letting me make up an exam?

How to show two or more non-overlapping label in a same position

Sci fi book about 20 + years old cant remember the author or title

Great Expectations [is written vs has been written] by Charles Dickens

Is this balcony with a protruding gargoyle a real place?

How did the general population of England convert so smoothly to Protestantism?

How can I avoid coming across as condescending when telling a coworker I cant work on an issue right now?

Why do some GDPR emails require me to opt-out and some to opt-in?

How were files transferred between different systems in the late 1980s?

Argsorting values in a list of lists

Why do we only use Soyuz to send humans to the ISS?

Why is slip necessary in an Induction motor?

How compelling is the hypothesis that dinosaurs were killed by toxic plants?

Identifying extra-galactic artifacts

Do writers write philosophical essays?

site design / logo 2018 Stack Exchange Inc; user contributions licensed undercc by-sa 3.0withattribution required.rev2018.5.25.30529