Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I force a sign-character on the output of an NSNumberFormatter
    primarykey
    data
    text
    <p>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 <strong>"%+.1f"</strong> does in <em>printf()</em>, that is always have a sign specified. </p> <pre><code>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]]]; </code></pre> <p>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 <code>setPositiveFormat:</code> and <code>setPositivePrefix:</code>.</p> <p>But I <em>don't</em> 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?</p> <p>What I do currently is:</p> <pre><code>label.text = [NSString stringWithFormat: @"XXX %s%@ XXX", (val &lt; 0) ? "" : "+", [nf stringFromNumber: [NSNumber numberWithDouble: val]]]; </code></pre> <p>But this presumes that '+' and '-' are correct for all formats.</p> <p>I'm sure it must be there since it is a standard formatting thing that has been in <em>printf()</em> since the dark ages...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload