Stop NSString localizedStringWithFormat from putting comma - ios

NSString localizedStringWithFormat puts in comma for separating thousands. How do you keep it from doing that. For example return 5000.25 instead of 5,000.25 (only have decimal separator)

localizedStringWithFormat uses the system's locale to format the numbers. What you want is to override the default locale - use initWithFormat:locale: for that. Basically, it's the same function, but you also supply the locale for which to format the text.
Also, keep in mind that initWithFormat:locale: also retains the string (as opposed to localizedStringWithFormat.
Check both of them here https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

Managed to get the desired behavior by using NSNumberFormatter and setting its groupingSize property to 0. Then used stringFromNumber method.

Related

localizedStringWithFormat is showing space for NSNumber object in French language

I am supporting few languages in my project including French.
self.lblMax.text=[NSString localizedStringWithFormat:#"%#%#",[NSNumber numberWithFloat:[self.arrValues.lastObject floatValue]],self.strUnit];
In above code line, arrValues is having list of [NSNumber numberWithFloat:val] objects. I am taking lastObject as the maximum number and appending its unit through localizedStringWithFormat method of NSString.
Let say, lastobject of arrValues array is 24000 So I am getting value "24 000ms" (with space) instead of normal "24000ms"
Since I had already set french language and France region both.
Sometimes It was showing comma "24,000ms" in UILabel but the actual value was 24000 only. I used stringByReplacingOccurence method to remove comma.
Kindly assist and correct me If I am doing something wrong for french localization.
Thanks in advance,
Showing the number 24000 as 24 000 is perfectly normal in some locales, including France. Some locales show it as 24,000. Some show it as 24.000. Some may show it other ways. There is nothing wrong with what you are seeing.
You may wish to consider using NSNumberFormatter to format numbers.
If your app is only supporting iOS 10 and later, you should look into using NSMeasurement and NSMeasurementFormatter instead of creating your own format string to ensure your number and units are really shown properly for a given locale.

How to display the dot of a decimal based on the locale but without being in a decimal number?

I have a calculator-like with obviously number buttons and a . button.
How can I display it depending on the local used knowing it's in the end more like a string than a decimal, so I guess I cannot use NSNumberFormatter for that.
Is there a way to do it without "translating" it for every language that I will use (which will be way less than there are locales)?
I thought of creating a formatted number like 0.1 using the locale, format it into string and keep only the . it its locale version but I guess there are proper ways
Thanks
Here is a way for it to work, I don't know if it's the best way...
let decimalSeparator = NSLocale.currentLocale().objectForKey(NSLocaleDecimalSeparator) as? String

Why we should use uppercaseStringWithLocale of NSString to get correct uppercase string?

I have a very simple task: from server I get UTF-8 string as byte array and I need to show all symbols from this string in upper case. From this string you can get really any symbol of unicode table.
I know how to do it in a line of code:
NSString* upperStr = [[NSString stringWithCString:utf8str encoding:NSUTF8StringEncoding] uppercaseString];
And seems to me it works with all symbols which I have checked. But I don't understand: why we need method uppercaseStringWithLocale? When we work with unicode each symbol has unique place in unicode table and we can easily find does it have upper/lower case representation. What trouble I might have if I use uppercaseString instead uppercaseStringWithLocale?
Uppercasing is locale-dependent. For example, the uppercase version of "i" is "I" in English, but "İ" in Turkish. If you always apply English rules, uppercasing of Turkish words will end up wrong.
The docs say:
The following methods perform localized case mappings based on the
locale specified. Passing nil indicates the canonical mapping. For
the user preference locale setting, specify +[NSLocale currentLocale].
Assumedly in some locales the mapping from lowercase to uppercase changes even within a character set. I'm not an expert in every language around the globe, but the people who wrote these methods are, so I use 'em.

using nsattributedstring and nslocalizedstring

My old code uses NSLocalizedString to display the following where outputText was an NSMutableString that contained many such lines in a single output session:
[outputText appendFormat: NSLocalizedString(#"\n\n%# and %# are identical. No comparison required.", #"\n\n%# and %# are identical. No comparison required."), self.ipAddress, secAddress.ipAddress];
I'm trying to change the color of the various ipAddress strings, but can't find a similar method when using NSMutableAttributedString.
The biggest problem I'm facing is that since this entire string will be localized, I can't reliably set the NSRange without breaking up each part of the formatted output.
Do I need to dissect each part of this string, convert it to NSAttributedString and append each piece to the outputText??
The answer is: yes.
Yes, you need to localize sections with different attributes separately.

How to disable scientific notation in .AsString in Delphi?

Hi
I want to get numbers from database, for example, if the number in database is 44.7890000000, I would like to get a string 44.789, the same 0.0010000000 -> 0.001, just keep the numbers and trim the tailing '0'.
I use this code:
qrySth.Fields[i].AsString - it does its job but I find for very small numbers like 0.0000010000 it becomes 1E-6. Is there a way I could disable the scientific notation for this AsString method?
Thanks!
As an alternative to setting the field's DisplayFormat property, you can read from AsFloat and pass the value directly to FormatFloat. It uses the same format pattern.

Resources