NSNumberFormatter with significant digits formats 0.0 incorrectly - objective-c

I've got an NSNumberFormatter set up to format using significant digits, so it should only show as many decimal digits as are necessary to correctly display the value.
When it is used to format 7.0 it works exactly as expected and produces #"7", but when it is used to format 0.0 the formatter produces #"0.0" instead of #"0" as would be expected.
NSNumberFormatter *doubleValF = [[NSNumberFormatter alloc] init];
[doubleValF setNumberStyle:NSNumberFormatterDecimalStyle];
[doubleValF setRoundingMode:NSNumberFormatterRoundHalfUp];
doubleValF.maximumFractionDigits = 9;
doubleValF.minimumFractionDigits = 0;
doubleValF.minimumSignificantDigits = 0;
doubleValF.maximumSignificantDigits = 30;
[doubleValF setUsesSignificantDigits:YES];
double value1 = 0.0;
NSString *value1String = [doubleValF stringFromNumber:[NSNumber numberWithDouble:value1]];
double value2 = 7.0;
NSString *value2String = [doubleValF stringFromNumber:[NSNumber numberWithDouble:value2]];
NSLog(#"value1=%#", value1String);
NSLog(#"value2=%#", value2String);
When I run this code, I get the following output:
2012-12-15 15:57:03.425 StackOverflowTests[41701:11303] value1=0.0
2012-12-15 15:57:03.426 StackOverflowTests[41701:11303] value2=7

Following David's LaMacchia's post, just check for a zero value and set the sig fig mode:
if(fabs(value) < 1e-6) {
self.numberLabelFormatter.usesSignificantDigits = NO;
}
else {
self.numberLabelFormatter.usesSignificantDigits = YES;
}
self.numberLabel.text = [self.numberLabelFormatter stringFromNumber:[NSNumber numberWithDouble:value]];
This may not be the correct method, but it works for me.

The issue is related to your having set usesSignificantDigits to YES. I believe the trailing digit, in 0.0, is considered significant.
There's a good discussion of a related issue here: https://stackoverflow.com/a/13110633/1435955

Try to set zeroSymbol property of number formatter to #"0".
doubleValF.zeroSymbol = #"0";

Related

NSDecimalNumber addition losing decimal places

I am trying to make a calculator iphone app and my teacher said we had to use NSDecimalNumber. I am having a lot of trouble with it. I am trying to get the addition part of it right, but I am having trouble when adding a number like 1.24 to a whole number like 33. The result comes out to be 34 when I want it to be 34.24. Does anyone know how to make it so it comes out this way? Here is the relevant parts of the code
-(void)outputNumber: (NSDecimalNumber*) number
{
//used to format number of decimal places
NSNumberFormatter* formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setMaximumFractionDigits:self.afterDecimal];
//output the number to calculator
NSString* formatNumber = [formatter stringFromNumber:[NSNumber numberWithDouble:[number doubleValue]]];
self.inputLabel.text = [NSString stringWithFormat: #"%#", formatNumber];
if (self.isWaiting == TRUE) {
self.numberB = number;
//change button title default color
[self.myButton setTitleColor:[UIColor colorWithRed:.196 green: .3098 blue: .52 alpha: 1] forState: 0];
}
else {
self.numberA = number;
}
}
if (self.binaryTag == 6) {
self.numberC = [self.numberB decimalNumberByAdding: self.numberA];
}
Actually NSNumberFormatter doesn't work correctly with NSDecimalNumber. It converts everything to a double first.
When using NSDecimalNumber, try to use its own methods to round it and then convert it into NSString. With a NSNumberFormatter you will just lose the precision.

How do I force a sign-character on the output of an NSNumberFormatter

I want to use a number formatter to generate my output, so the number is automatically formatted for the user's locale, but I want it to work like "%+.1f" does in printf(), that is always have a sign specified.
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.numberStyle = NSNumberFormatterDecimalStyle;
nf.maximumFractionDigits = 1;
double val = 3.1234;
label.text = [NSString stringWithFormat: #"XXX %# XXX", [nf stringFromNumber: [NSNumber numberWithDouble: val]]];
I want the label to come out "XXX +3.1 XXX" in the US and the appropriate but equivalent string for any other location. The only things I can find are setPositiveFormat: and setPositivePrefix:.
But I don't want to set the format since I don't know how to format numbers in other countries; I don't know if a plus-sign is used to designate a positive number in Arabic or Russian or some culture I have not thought of. I do know, for example, that decimal points, commas, spaces, etc., all have different meanings in European countries compared to the U.S. - Could the same be true for +/- signs?
What I do currently is:
label.text = [NSString stringWithFormat: #"XXX %s%# XXX", (val < 0) ? "" : "+",
[nf stringFromNumber: [NSNumber numberWithDouble: val]]];
But this presumes that '+' and '-' are correct for all formats.
I'm sure it must be there since it is a standard formatting thing that has been in printf() since the dark ages...
How about this:
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.numberStyle = NSNumberFormatterDecimalStyle;
nf.maximumFractionDigits = 1;
double val = 3.1234;
NSString *sign = (val < 0) ? [nf minusSign] : [nf plusSign];
NSString *num = [nf stringFromNumber:#(abs(val))]; // avoid double negative
label.text = [NSString stringWithFormat: #"XXX %#%# XXX", sign, num];
You may need to check to see if num has the sign prefix or not so it isn't shown twice.
Edit: After some playing around, it has been determined, for the "Decimal" style, that no current locale uses a positivePrefix. No current locale uses a plusSign other than the standard + character. No current locale uses a negativePrefix that is different than minusSign. No current locale uses either positiveSuffix or negativeSuffix.
So an easier approach would be to do:
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.numberStyle = NSNumberFormatterDecimalStyle;
nf.maximumFractionDigits = 1;
[nf setPositivePrefix:[nf plusSign]];
[nf setNegativePrefix:[nf minusSign]];
label.text = [nf stringFromNumber:#(val)];
This case it's simple, just add the prefix:
nf.positivePrefix= nf.plusSign;
Though it won't use the user's locale, you can do the following to generate the +/- sign without the somewhat expensive overhead of an NSNumberFormatter:
// assume 'number' is an NSNumber
label.text = [NSString stringWithFormat:#"%+.02f", [number floatValue]];
Simple Case:
let f = NumberFormatter()
f.positivePrefix = f.plusSign
Currency Case :
Hack needed, because setting the prefix to plusSign only will remove the currency symbol.
let f = NumberFormatter()
f.numberStyle = .currency
f.positivePrefix = f.plusSign + f.currencySymbol
There is a bit more work depending on the locale.. The currency symbol may be before, or after, but this is probably another subject..
Edit:
Even if it is another subject, I'd say a possible solution to the problem above is to subclass NSNumberFormatter :
override func string(from number: NSNumber) -> String? {
returns ( number.doubleValue >= 0 ? super.plusSign : "" ) + super.string(from: number)
}
This way, NSNumberFormatter should manage the currency position while your subclass simply prepend the + sign. No time to test this in depth, but at least it is an approach.
The underlying formatting language for NSNumberFormatter doesn't have any provision for what you want to do -- it will allow you to specify a localized positive sign on exponents, but not for the entire formatted string. Nor does NSLocale seem to make available the localized positive sign.
Aside from making a dummy string that includes an exponent, pulling the localized positive sign out, and putting your final formatted string together by hand, I think you're out of luck.
A reusable formatter in swift:
var numberFormatter: NSNumberFormatter {
let formatter = NSNumberFormatter()
formatter.numberStyle = .DecimalStyle
formatter.locale = NSLocale(localeIdentifier: "it_IT")//your Locale
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 0
formatter.positivePrefix = formatter.plusSign
return formatter
}
Then use it:
let myDoubleValue = 12.00
let myStringNumber = numberFormatter.stringFromNumber(myDoubleValue)!
I don't think any of the previous answers will actually take into consideration everything you mentioned in your question.
It is true that NumberFormatter does not have an option to set the plus sign visible for all positive numbers when formatting currency values.
Also, replacing prefixes and suffixes will likely break the format for some regions and always replacing a prefix will only work if the set locale uses the currency symbol on the left.
A simple way to address this without losing the locale formatting can be seen below:
var value: Double = 3.1234
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.maximumFractionDigits = 1
if value > 0 {
return formatter.string(for: value.negated())?.replacingOccurrences(
of: formatter.minusSign,
with: formatter.plusSign
)
} else {
return formatter.string(for: value)
}
Even though this can be seen as hack, it's an effective way to achieve everything you mentioned without manually writing a number formatter.

How to trim zeros after decimal point

I am trying to trim zeros after a decimal point as below but it's not giving desired result.
trig = [currentVal doubleValue];
trig = trig/100;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:0];
display.text = [formatter stringFromNumber:[NSNumber numberWithDouble:trig]];
The number is still being displayed without trimming zeros after the decimal point.
Here currentVal is the number I am entering.
For example if i pass "trig" = 123 (Initially "trig" = 123 after doing trig/100 i want to display 1.23 but it is displaying as 1.23000000).
Sometimes the straight C format specifiers do an easier job than the Cocoa formatter classes, and they can be used in the format string for the normal stringWithFormat: message to NSString.
If your requirement is to not show any trailing zeroes, then the "g" format specifier does the job:
float y = 1234.56789f;
NSString *s = [NSString stringWithFormat:#"%g", y];
Notice that there is no precision information, which means that the printf library will remove the trailing zeroes itself.
There is more information in the docs, which refer to IEEE's docs.
In case this helps someone. I wanted 1 decimal value but no '.0' on the end if the float was '1.0'. Using %g would give scientific notation for longer numbers, following ugliness worked well enough for me as high accuracy wasn't critical.
// Convert to 1 dp string,
NSString* dirtyString = [NSString stringWithFormat: #"%.1f", self.myFloat];
// Convert back to float that is now a maximum of 1 dp,
float myDirtyFloat = [dirtyString floatValue];
// Output the float subtracting the zeros the previous step attached
return [NSString stringWithFormat: #"%g", myDirtyFloat];
This will not display any decimal value after the decimal point:
display.text = [NSString stringWithFormat:#"%1.0f", trig];
This will just trim the zeros after the decimal point:
isplay.text = [NSString stringWithFormat:#"%3.2f", trig];
display.text = [display.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:[NSString stringWithFormat#"0"]]];
Note, this may leave you with the trailing decimal point. "124." may happen. I guess that some smarter solution will be posted soon.
From the documentation, it looks like setFractionDigits: is only for converting the other way.
The best thing to do is probably to convert your number to an integer before formatting it e.g.
double converted = round(trig); // man round for docs
You can use also the formatting functions of stringWithFormat: of NSString, but then you will lose all the localisation advantages you get with NSNumberFormatter.
This may not be a proper solution where there is NSNumberFormetter Class, But I just did this rather then googling a lot! ;)
Here is an example, if it helps:
-(NSString*) trimZerosAfterDecimalPoint:(NSString*)string_ {
double doubleValue=[string_ doubleValue];
long leftPart=(long)doubleValue;
double rightPart=doubleValue-(double)leftPart;
NSString *rightPartAsStr=[NSString stringWithFormat:#"%f", rightPart];
int i=0;
for (i=rightPartAsStr.length-1; i>=2; i--) {
if ([rightPartAsStr characterAtIndex:i]!='0') {
rightPartAsStr=[rightPartAsStr substringWithRange:NSMakeRange(2, i-1)];
break;
}
}
if (i<2) {
string_=[NSString stringWithFormat:#"%ld", leftPart];
} else {
string_=[NSString stringWithFormat:#"%ld.%#", leftPart, rightPartAsStr];
}
return string_;
}
I just had to do this for one of my programs and heres how I went about it:
- (void) simplify{
int length = (int)[self.calcString length];
for (int i = (int)[self.calcString length]; i > 0; i--) {
if ([self.calcString rangeOfString:#"."].location != NSNotFound) {
NSRange prevChar = NSMakeRange(i-1, 1);
if ([[self.calcString substringWithRange:prevChar] isEqualToString:#"0"]||
[[self.calcString substringWithRange:prevChar] isEqualToString:#"."])
length--;
else
break;
}
self.calcString = [self.calcString substringToIndex:length];
}
}
This works
display.text = [#(trig) stringValue];
it is because of your datatype cannot be formatted is such a manner.

Rounding numbers in Objective-C

I'm trying to do some number rounding and conversion to a string to enhance the output in an Objective-C program.
I have a float value that I'd like to round to the nearest .5 and then use it to set the text on a label.
For example:
1.4 would be a string of: 1.5
1.2 would be a string of: 1
0.2 would be a string of: 0
I've spent a while looking on Google for an answer but, being a noob with Objective-C, I'm not sure what to search for! So, I'd really appreciate a pointer in the right direction!
Thanks,
Ash
Thanks for the pointers everyone, I've managed to come up with a solution:
float roundedValue = round(2.0f * number) / 2.0f;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:1];
[formatter setRoundingMode: NSNumberFormatterRoundDown];
NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:roundedValue]];
[formatter release];
The above works for the test cases I threw at it, but if anyone knows a better way to do this I'd be interested to hear it!
float floatVal = 1.23456;
Rounding
int roundedVal = lroundf(floatVal);
NSLog(#"%d",roundedVal);
Rounding Up
int roundedUpVal = ceil(floatVal);
NSLog(#"%d",roundedUpVal);
Rounding Down
int roundedDownVal = floor(floatVal);
NSLog(#"%d",roundedDownVal);
NSString *numberString = [NSString stringWithFormat:#"%f", round(2.0f * number) / 2.0f];
Use lroundf() to round a float to integer and then convert the integer to a string.
NSString *numberString = [NSString stringWithFormat:#"%d",lroundf(number)];
I'd recommend looking into using NSNumberFormatter.
a simple way:
float theFloat = 1.23456;
int rounded = roundf(theFloat); NSLog(#"%d",rounded);
int roundedUp = ceil(theFloat); NSLog(#"%d",roundedUp);
int roundedDown = floor(theFloat); NSLog(#"%d",roundedDown);
// Note: int can be replaced by float
NSString *intNumberString = [NSString stringWithFormat:#"%d", (int)floatNumber];
Following Technique worked for me in a financial application.
NSString *dd=[NSString stringWithFormat:#"%0.2f",monthlyPaymentCalculated];
monthlyPaymentCalculated=[dd doubleValue];
self.monthlyPaymentCritical=monthlyPaymentCalculated;
what is did is first is rounded it with %0.2f and stored it in NSString then i simply converted it again into double and result was good for my calculation.
With these functions you can round to any value.. If you use p=2, you get even numbers.
float RoundTo(float x, float p)
{
float y = 1/p;
return int((x+(1/(y+y)))*y)/y;
}
float RoundUp(float x, float p)
{
float y = 1/p;
return int((x+(1/y))*y)/y;
}
float RoundDown(float x, float p)
{
float y = 1/p;
return int(x*y)/y;
}
I needed to be able to round to a specific digit (not necessarily to whole integers). I made a NSNumber category (based off Ash's answer) and added the following method to it:
- (NSString *)stringByRounding:(NSNumberFormatterRoundingMode)roundingMode
toPositionRightOfDecimal:(NSUInteger)position
{
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:position];
[formatter setRoundingMode:roundingMode];
NSString *numberString = [formatter stringFromNumber:self];
return numberString;
}
Which allows me to use it like so:
[aNumber stringByRounding:NSNumberFormatterRoundUp toPositionRightOfDecimal:2];
I can use it to round to integers by passing in 0 as the second parameter:
[aNumber stringByRounding:NSNumberFormatterRoundPlain toPositionRightOfDecimal:0];

Make a float only show two decimal places

I have the value 25.00 in a float, but when I print it on screen it is 25.0000000.
How can I display the value with only two decimal places?
It is not a matter of how the number is stored, it is a matter of how you are displaying it. When converting it to a string you must round to the desired precision, which in your case is two decimal places.
E.g.:
NSString* formattedNumber = [NSString stringWithFormat:#"%.02f", myFloat];
%.02f tells the formatter that you will be formatting a float (%f) and, that should be rounded to two places, and should be padded with 0s.
E.g.:
%f = 25.000000
%.f = 25
%.02f = 25.00
Here are few corrections-
//for 3145.559706
Swift 3
let num: CGFloat = 3145.559706
print(String(format: "%f", num)) = 3145.559706
print(String(format: "%.f", num)) = 3145
print(String(format: "%.1f", num)) = 3145.6
print(String(format: "%.2f", num)) = 3145.56
print(String(format: "%.02f", num)) = 3145.56 // which is equal to #"%.2f"
print(String(format: "%.3f", num)) = 3145.560
print(String(format: "%.03f", num)) = 3145.560 // which is equal to #"%.3f"
Obj-C
#"%f" = 3145.559706
#"%.f" = 3146
#"%.1f" = 3145.6
#"%.2f" = 3145.56
#"%.02f" = 3145.56 // which is equal to #"%.2f"
#"%.3f" = 3145.560
#"%.03f" = 3145.560 // which is equal to #"%.3f"
and so on...
You can also try using NSNumberFormatter:
NSNumberFormatter* nf = [[[NSNumberFormatter alloc] init] autorelease];
nf.positiveFormat = #"0.##";
NSString* s = [nf stringFromNumber: [NSNumber numberWithFloat: myFloat]];
You may need to also set the negative format, but I think it's smart enough to figure it out.
I made a swift extension based on above answers
extension Float {
func round(decimalPlace:Int)->Float{
let format = NSString(format: "%%.%if", decimalPlace)
let string = NSString(format: format, self)
return Float(atof(string.UTF8String))
}
}
usage:
let floatOne:Float = 3.1415926
let floatTwo:Float = 3.1425934
print(floatOne.round(2) == floatTwo.round(2))
// should be true
In Swift Language, if you want to show you need to use it in this way. To assign double value in UITextView, for example:
let result = 23.954893
resultTextView.text = NSString(format:"%.2f", result)
If you want to show in LOG like as objective-c does using NSLog(), then in Swift Language you can do this way:
println(NSString(format:"%.2f", result))
IN objective-c, if you are dealing with regular char arrays (instead of pointers to NSString) you could also use:
printf("%.02f", your_float_var);
OTOH, if what you want is to store that value on a char array you could use:
sprintf(your_char_ptr, "%.02f", your_float_var);
The problem with all the answers is that multiplying and then dividing results in precision issues because you used division. I learned this long ago from programming on a PDP8.
The way to resolve this is:
return roundf(number * 100) * .01;
Thus 15.6578 returns just 15.66 and not 15.6578999 or something unintended like that.
What level of precision you want is up to you. Just don't divide the product, multiply it by the decimal equivalent.
No funny String conversion required.
in objective -c is u want to display float value in 2 decimal number then pass argument indicating how many decimal points u want to display
e.g 0.02f will print 25.00
0.002f will print 25.000
Here's some methods to format dynamically according to a precision:
+ (NSNumber *)numberFromString:(NSString *)string
{
if (string.length) {
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
f.numberStyle = NSNumberFormatterDecimalStyle;
return [f numberFromString:string];
} else {
return nil;
}
}
+ (NSString *)stringByFormattingString:(NSString *)string toPrecision:(NSInteger)precision
{
NSNumber *numberValue = [self numberFromString:string];
if (numberValue) {
NSString *formatString = [NSString stringWithFormat:#"%%.%ldf", (long)precision];
return [NSString stringWithFormat:formatString, numberValue.floatValue];
} else {
/* return original string */
return string;
}
}
e.g.
[TSPAppDelegate stringByFormattingString:#"2.346324" toPrecision:4];
=> 2.3453
[TSPAppDelegate stringByFormattingString:#"2.346324" toPrecision:0];
=> 2
[TSPAppDelegate stringByFormattingString:#"2.346324" toPrecision:2];
=> 2.35 (round up)
Another method for Swift (without using NSString):
let percentage = 33.3333
let text = String.localizedStringWithFormat("%.02f %#", percentage, "%")
P.S. this solution is not working with CGFloat type only tested with Float & Double
Use NSNumberFormatter with maximumFractionDigits as below:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.maximumFractionDigits = 2;
NSLog(#"%#", [formatter stringFromNumber:[NSNumber numberWithFloat:12.345]]);
And you will get 12.35
If you need to float value as well:
NSString* formattedNumber = [NSString stringWithFormat:#"%.02f", myFloat];
float floatTwoDecimalDigits = atof([formattedNumber UTF8String]);
lblMeter.text=[NSString stringWithFormat:#"%.02f",[[dic objectForKey:#"distance"] floatValue]];