My application is allowing user to select various currencies for spendings tracking.
I have a label which displays the amount with currecy symbol. I'm using NSNumberFormatter with kCFNumberFormatterCurrencyStyle to format the amount string and display it in the label;
numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = kCFNumberFormatterCurrencyStyle;
numberFormatter.currencyCode = #"EUR";
My goal is to display the currency symbol with different color, so i'm using NSAttributedString, trying to find symbols' range and set different attributes to it. The problem is that the formatter return wrong symbol when I initilizing the attributed string:
MLOG(#"internationalCurrencySymbol %#", numberFormatter.internationalCurrencySymbol);
MLOG(#"currencySymbol %#", numberFormatter.currencySymbol);
MLOG(#"currencyCode %#", numberFormatter.currencyCode);
//logs:
//USD
//$
//EUR
but when the label is displayed on the screen I see correct Euro currency symbol: €
Does anybody know how can get the currency symbol for given currency code?
I just tested your code. If you run the log statements right after defining the number formatter the way you did, the output is
#"EUR"
#"€"
#"EUR"
Note that international currency symbol and currency symbol seem to depend on the locale set in your system. But you can easily change the locale for the formatter like this:
numberFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"de_DE"];
It would probably be best to not set the currencySymbol at all. Then when the locale is, say, Thailand (#"th_TH"), you get
numberFormatter.internationalCurrencySymbol "THB"
numberFormatter.currencySymbol "฿"
numberFormatter.currencyCode "THB"
Related
I'm trying to get an accessibilityValue with a decimal number on a custom UIView to readout as "twenty point one", for example, similar to how voice over reads out the duration and keyframe values on the video trimmer when editing a video in the Photos app.
The default setup reads out the value as "twenty dot one". If I set the accessibilityAttributedLabel instead using the accessibilitySpeechPunctuation key, it reads as "twenty period one".
view.accessibilityAttributedLabel = NSAttributedString(string: "20.1", attributes: [.accessibilitySpeechPunctuation: true])
Without resorting to manually building a numeric string to read out, anyone know how to get the number to read saying "point" instead of "dot" or "period"?
Got it! Formatting a number using a NumberFormatter with a style of .spellOut will generate a string with the fully spelled out value. Not what we want for a label's text, but exactly what we want for an accessibility label.
let formatter = NumberFormatter()
formatter.numberStyle = .spellOut
let label = UILabel()
label.text = formatter.string(from: 20.1)
label.accessibilityLabel = formatter.string(from: 20.1)
// prints out "twenty point one"
print(label.accessibilityLabel)
I have a NSNumberFormatter and I would like to display currency as in a Billion,
So if I have 1,000,000,000 $ I would like to display 1B $
I create NSNumberFormatter object and set multiplyer :
NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setMultiplier: #(1.0/1000000000)];
If I display multiplier as:
NSLog(#" NUMBER %#", numberFormatter.multiplier); // --> NUMBER 1e-09
But if I put
NSLog(#"Formatter %#", [numberFormatter1 stringFromNumber:#(1225245041496000)]); // --> Formatter 0
It seams to me that multiplier round number but I don't know how to prevent this.
Looks like you're exceeding the minimum value allowed for the multiplier. I can't find docs on it, but quick testing shows:
[numberFormatter setMultiplier: #(1.0 / 8388608.0)];
works fine, but
[numberFormatter setMultiplier: #(1.0 / 8388609.0)];
fails (string output is always "0").
So I don't think NSNumberFormatter is going to fit your needs.
I have been trying to find a way to find a way to append commas to a string or float. I have a float which could be anything from 10.50 to 50000000000.99 and would love to be able to format this to add commas to every 3 digits (not including the floats). I know that there is a NSNumberFormatter that can use NSNumberFormatterDecimalStyle or NSNumberFormatterCurrencyStyle but neither of those work with strings/floats. I have tried to split the float into two strings and then try to do a % of length == 3 calculation but it began to get really really messy. I did something similar in Java but it seems to be a lot harder in iOS.
If anyone have any suggestions or ideas? Any information or guidance is appreciated! Thanks in advance.
Edit: I know there are posts out there but none of them seem to solve the problem with doubles/floats/strings that end in zeros. For example, if I have a double as 16.00, the formatter will format that into 16. Same thing goes for a 1234.80. The formatter will format it into 1,234.8 instead of 1,234.80 and that is what I am looking for.
View this post on NSNumberFormatting here
The gist of it is to covert the float or double to a NSNumber and then use the NSNumberFormatter on the NSNumber to get a NSString with the desired format. Something like:
double doubleNum = 1000.002;
// Change double to nsnumber:
NSNumber *num = [NSNumber numberWithDouble:doubleNum];
// Set up the formatter:
NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
[numFormatter setUsesGroupingSeporator:YES];
[numFormatter setGroupingSeparator:#","];
[numFormatter setGroupingSize:3];
// Get the formatted string:
NSString *stringNum = [numFormatter stringFromNumber:num];
NSLog(#"%#",stringNum);
// prints out '1,000.002'
Give it a try, there are lots of different formatting settings you can apply.
Read Apple's guide to formatting numbers, NSNumberFormatter supports both thousand separators and fractional parts. For example see Listing 1 in the referenced doc.
I have app with three view controllers. First is UIViewController with three UITextField's where user put some digits. These digits are stored to my CoreData entity as String attributes.
Second is UITableView, where I show my stored data from CoreData as new cell.
Third is detail UIViewController where I show user all his previously inserted digits.
The problem is when I set on textField decimal pad, user have digits and comma but my function need digits with dot for double precision calculating.
With comma I can't make mathematical function as [.....] * [...] = .... because it doesn't work.
Any idea how I can figure it out?
Can I simply change that comma to a dot?
I have this code:
NSNumberFormatter*nf = [[NSNumberFormatter alloc]init];
[nf setNumberStyle:NSNumberFormatterDecimalStyle];
double result = [myString doubleValue] * [myOtherString doubleValue];
You want the decimal keypad to have either a comma or a period (dot) based on the user's locale. This is so the user can enter the value as they are accustomed to. In order for you to do the math with the vales, you need to use a NSNumberFormatter to convert the entered string into a double value. Once you do this, your math formulas will work. Never use doubleValue or floatValue to convert an NSString to a double or float if the string was entered by the user. Always use NSNumberFormatter to properly deal with the user's locale.
Update based on code added to question.
You don't use the number formatter. You are doing exactly what I said not to do. Change your code to:
double firstValue = [[nf numberFromString:myString] doubleValue];
double secondValue = [[nf numberFromString:myOtherString] doubleValue];
double result = firstValue * secondValue;
Solution is very simple:
1. Go to Settings > General > International > Regional Format
2. Now select "United States"
Now run your App and see dot(.) instead of comma(,) in Decimal pad keyboard.
My objective is to create a customer calculator application for iPhone and I am using Xcode to write my application. My problem, that I cannot find a solution for, is how to format a number that uses decimals (with extra zeros) without switching into scientific notation
I tried...
buttonScreen.text = [NSString stringWithFormat:#"%0.f",currentNumber];
%0.f formatting always rounds so if the user types in "4.23" it displays "4"
%f formats numbers with 6 decimals (typing in '5' displays as '5.000000'), but I don't want to show extra zeros on the end of the number.
%10.4f is something else that I have seen in my reading to find the solution, but my problem is that I don't know how many decimals will be in the answer, and I may want zero decimals or 10 decimals depending on the number.
The following are examples of numbers I'd like to display (without the commas): A whole number larger than 6 digits, a decimal number with more than 6 digits.
123,456,789;
0.123456789;
12345.6789;
-123,456,789;
-0.23456789;
-12345.6789;
*This is a spiritual repost to my earlier question "How to Format Numbers without scientific notation or decimals" which I poorly phrased as I intended to write 'unnecessary (extra zeros),' but upon rereading my post clearly witnessed my inability to convey that at any point in my question.
Use the NSNumberFormatter class.
First define the formatter:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
Then you can define various properties of the formatter:
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.maximumIntigerDigits = 3;
formatter.minimumFractionDigits = 3;
formatter.maximumFractionDigits = 8;
formatter.usesSignificantDigits = NO;
formatter.usesGroupingSeparator = YES;
formatter.groupingSeparator = #",";
formatter.decimalSeparator = #".";
....
You format the number into a string like this:
NSString *formattedNumber = [formatter stringFromNumber:num];
Play around with it. Its pretty simple, but may take some work to get the look you would like.
Actually, it makes more sense to use this:
label.text = [NSString stringWithFormat:#"%.4f", answer];
This tells XCode to display your number with 4 decimal places, but it doesn't try to "pad" the front of the number with spaces. For example:
1.23 -> " 1.2300" // When using [NSString stringWithFormat:#"%9.4f", answer];
1.23 -> "1.2300" // When using [NSString stringWithFormat:#"%.4f", answer];
try something like this
label.text = [NSString stringWithFormat:#"%9.4f", answer];
where the 9 means total digits (in terms of padding for alignment), and the 4 means 4 decimal places.