0

I have a simple calculation

float x = ([_enterValue.text floatValue]);
//known constant number
float y = (My value here);
[_resultTxt setText:[NSString stringWithFormat:@"%f", x * y]];

The resulting stringWithFormat has a lot of zeros ie. 1.806700000000

How would I go about formatting this so my string is simply something like 1.8

2
  • 2
    Do not use stringFormat for decimal numbers shown to a user. You really should use NSNumberFormatter so the number is properly formatted for the user's locale. Many people in Europe, for example, want to see 1,8, not 1.8. Commented Dec 17, 2012 at 19:24
  • Thanks for the tip Maddy, im in UK and we always use the decimal point but thats a good heads up for future apps, thanks Commented Dec 17, 2012 at 19:44

1 Answer 1

2

Try this:

[NSString stringWithFormat:@"%.1f", x * y]

The number after the decimal denotes the number of decimal places to be printed.

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

1 Comment

Wow that was quick and really helpful! Thanks for explaining it to, will mark as correct when it lets me, plus 1 for fast answer!

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.