1

When formatting a number into a string, I can limit the characters after a decimal point with, e.g., %.03f (display three digits after the point), but how do I also remove all digits BEFORE the point?

For example, I'd like 100.943 to format to ".943".

4 Answers 4

2

I think the only way for you to do that would be to remove the number before the decimal point using math, or string manipulation after the fact, like componentsSeparatedByString:@"."

A simple math way of removing it would be:

float initialValue = 100.943f;
int beforeDecimal = (int)initialValue;
float afterDecimal = initialValue - beforeDecimal;
Sign up to request clarification or add additional context in comments.

4 Comments

With this method I could skip the math part and simply extract a substring. I was wondering if there was a format that could be used to do this automatically. Thanks anyways.
I don't think there is a way for you to format strings that way unfortunately.
For your future reference, here is a link to the NSString formatting guidelines: developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/…
Going to mark yours as correct, as I believe there isn't a way to format either. Thanks for your help.
2

Use an NSNumberFormatter, and set its maximumIntegerDigits to 0:

float f = 123.456;
NSNumber * nF = [NSNumber numberWithFloat:f];
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumIntegerDigits:0];
NSLog(@"%@", [formatter stringFromNumber:nF]);

2011-11-23 18:36:59.138 NoIntegerDigits[21834:903] .456

2 Comments

Upvote for you...but it's still not really part of NSString stringWithFormat. I ended up doing [resultString substringFromIndex:i]; once I determined where the '.' was.
stringWithFormat: is not designed to do intricate formatting tasks; it's just a quick way to get primitives into an NSString. The fact that specifiers such as %.03 work is probably due to it being built on sscanf. If you have specific formatting needs, use a formatter object.
1

Ok I guess i got your question now..

Here is another solution where you will get strings out of string only.

example:

NSString *floatAsString = @"123.456";
NSArray stringDivisions = [floatAsString componentsSeparatedByString:@"."];

Here what you will get in array is: 123 and 456 to separate string objects. Once you get two separate strings, you can play with them.

This is what you needed ?

Comments

0

Simple way to do that, Here is an example,

Firstly convert your string to float value.

float x = 102.943;

int y = x;

float ans = x % y;

NSLog(@"\nx : %f \ny : %d \nans : %f", x, y, ans);

So the output will be:

x : 102.943000

y : 102

ans : 0.943000

Hope this is what you needed.

1 Comment

Actually looking for a way to format the string output for a UILabel without the whole numbers, but thanks anyways.

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.