2

I have such a problem: double calcValue;

NSString *buf = [NSString stringWithFormat:@"%14f", myCalculator.calcValue];

buf = "7.142857";

istead of

buf = "7.1428571428571423";

I tried to format it in such ways: @"%f". But when I tried @"%14.9f" 9 signs after comma displayed.

Whats the problem? And how can I solve it? Thanx!

0

2 Answers 2

3

Unless I'm misunderstanding, the correct format would be @"%.14f.

What you did, @"%14f" says you want up to 14 digits, before the decimal, but you didn't specify digits after.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure why, but stringWithFormat: doesn't seem to format doubles properly. You might try this approach:

double calcValue=7.1428571428571423;
NSString *buf=[[NSNumber numberWithDouble:calcValue] stringValue];

which will set buf to @"7.142857142857142".

For tighter control over number of digits, you could use a NSNumberFormatter.

1 Comment

the answer is using "%g". it do all things together!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.