When multiplying two large numbers I am getting exponential values.
double price=[priceperUnit.text doubleValue];
double quantity1=[quantity.text doubleValue];
double totalvalue=price * quantity1;
totalValue.text=[NSString stringWithFormat:#"%.2f", totalvalue];
[dic setObject:#([totalValue.text floatValue]) forKey:#"Total Value"];
In dictionary I am getting these values:
PriceperUnit = 1485500;
Quantity = 9;
TotalValue = "1.33695e+07";
How to solve this issue?
As always, you should be using NSNumberFormatter for every number that you want to display to users:
double price = 1485500;
double quantity1 = 9;
double totalvalue = price * quantity1;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.minimumFractionDigits = 2;
formatter.maximumFractionDigits = 2;
totalValue.text = [formatter stringFromNumber:#(totalvalue)];
[dic setObject:#(totalvalue) forKey:#"Total Value"];
Also note that it is not a good practice to use double with money values. Operations with double are not suited for exact calculations. You should use NSDecimalNumber instead.
Related
I need to make NSNumber to display only 4 decimal points. This part of code is works, but it outputs result without leading zero.
double resultRoundToDecimal = [result doubleValue];
NSNumberFormatter *resultFormatter = [[NSNumberFormatter alloc] init];
[resultFormatter setRoundingMode:NSNumberFormatterRoundHalfUp];
[resultFormatter setMaximumFractionDigits:4];
resultData = [resultFormatter stringFromNumber:[NSNumber numberWithDouble:resultRoundToDecimal]];
For example:
1/3 = .3333
I want:
1/3 = 0.3333
How I can to do this?
You could choose to use string formatter too, like below
float val=1./3;
NSString *resultData=[NSString stringWithFormat:#"%0.4f",val];
NSLog(#"Result = %#",resultData);
Prepend a 0 or use number formatter.
NSString *printStr = #"0";
printStr = [NSString stringByAppendingString: resultData];
Otherwise, you could use a number formatter or something similar. If your just outputting a string why not do that?
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNumberFormatter_Class/Reference/Reference.html
I am trying to properly display properly formatted currencies from long values. I am using NSNumberFormatter however it seems to be cutting off my decimal places where the cents would go.
For example, if I have a long value of 1203 (cents) I want it to have a fixed point format (like 12.03). Here is what I have done:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.currencyCode = "USD";
formatter.multiplier = [NSNumber numberWithDouble:0.01];
long currencyAmount = 1203;
NSNumber *number = [NSNumber numberWithLongLong:currencyAmount];
[label setText:[formatter stringFromNumber:number]];
I am getting this output $12.00 but I want $12.03
To think of an integer cut-off bug inside of NSNumberFormatter is crazy speculation but have you tried the default multiplier of one and dividing your currency amount after conversion to float by 100 yourself?
EDIT: For this workaround the following post suggests the use of NSDecimalNumber to avoid rounding problems. NSNumberFormatter to format currency not working for floats
I figured out the answer. This will properly format a long/long long value to a currency.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.currencyCode = "USD";
long currencyAmount = 1203;
NSDecimalNumber *wrappedCurrencyAmount = [NSDecimalNumber decimalNumberWithMantissa:currencyAmount exponent:-2 isNegative:NO];
[label setText:[formatter stringFromNumber:wrappedCurrencyAmount]];
I am trying to round a floating value upto two decimal places. I am using objective-c
e.g 1.47567 should be like this , 1.47 .. Please help
Thnx .
float num = 1.47567;
num *= 100;
if(num >= 0) num += 0.5; else num -= 0.5;
long round = num;
num = round;
num /= 100;
NSLog(#"%.2f",num);
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
NSString *formattedNumber = [formatter stringFromNumber:#(self.speed)];
double value = 1.47567;
double roundedValue = round(value * 100.0) / 100.0;
Of course you can use a named constant in place of 100.0. This is just a demo.
Do this
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
NSString *formattedNumber = [formatter stringFromNumber:#(self.speed)];
float numTwoDecimalDigits = atof([formattedNumber UTF8String]);
NSLog(#"%.2f", 1.47567);
would round to two decimal places. If you want to "cut", there are different options. For example:
NSLog(#"%.2f", floor(1.47567 * 100) / 100);
i'm looking for a way to represent a double value in a NSString meeting the following format requirements:
1.) no trailing zeros: x = 0.5000000 -> #"0.5"
2.) at least on decimal: x = 45 -> #"45.0"
3.) maximum 8 decimal characters (last one rounded): x = 0.000000005 -> #"0.00000001"
I tried with #"%.8f", #"%g" or #"%.8g" but all fail for at least one of the requirements.
I think I could do with #"%.8f" and then loop the characters of the string beginning with the last character of string to the front deleting the "0" characters,
Or start with a "%.8g" and append a ".0" if the string does not contain a decimal point.
Is there any smarter solution available?
You can do this with a number formatter, but you need to create an NSNumber with your double first:
NSNumber *halfNumber = [NSNumber numberWithDouble:0.5000000];
NSNumber *wholeNumber = [NSNumber numberWithDouble:45];
NSNumber *longNumber = [NSNumber numberWithDouble:0.000000005];
NSNumberFormatter *formatter = [NSNumberFormatter new];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.roundingMode = NSNumberFormatterRoundHalfUp;
formatter.minimumFractionDigits = 1;
formatter.maximumFractionDigits = 8;
NSString *halfString = [formatter stringFromNumber:halfNumber];
NSString *wholeString = [formatter stringFromNumber:wholeNumber];
NSString *longString = [formatter stringFromNumber:longNumber];
NSLog(#"%#", halfString);
NSLog(#"%#", wholeString);
NSLog(#"%#", longString);
// Output is:
// 2012-11-12 04:22:41.181 Testing App[48069:fb03] 0.5
// 2012-11-12 04:22:41.182 Testing App[48069:fb03] 45.0
// 2012-11-12 04:22:41.183 Testing App[48069:fb03] 0.00000001
How do I round an NSNumber to zero decimal spaces, in the following line it seems to keep the decimal spaces:
NSNumber holidayNightCount = [NSNumber numberWithDouble:sHolidayDuration.value];
Typically casting to int truncates. For example, 3.4 becomes 3 (as is desired), but 3.9 becomes 3 also. If this happens, add 0.5 before casting
int myInt = (int)(sHolidayDuration.value + 0.5);
Here's a bit of a long winded approach
float test = 1.9;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setRoundingMode:NSNumberFormatterRoundHalfUp];
[formatter setMaximumFractionDigits:0];
NSLog(#"%#",[formatter stringFromNumber:[NSNumber numberWithFloat:test]]);
[formatter release];
If you only need an integer why not just use an int
int holidayNightCount = (int)sHolidayDuration.value;
By definition an int has no decimal places
If you need to use NSNumber, you could just cast the Double to Int and then use the int to create your NSNumber.
int myInt = (int)sHolidayDuration.value;
NSNumber holidayNightCount = [NSNumber numberWithInt:myInt];
you can also do the following: int roundedRating = (int)round(rating.floatValue);
Floor the number using the old C function floor() and then you can create an NSInteger which is more appropriate, see:
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/floor.3.html ....
NSInteger holidayNightCount = [NSNumber numberWithInteger:floor(sHolidayDuration.value)].integerValue;
Further information on the topic here: http://eureka.ykyuen.info/2010/07/19/objective-c-rounding-float-numbers/
Or you could use NSDecimalNumber features for rounding numbers.