Note that there are some explanatory texts on larger screens.

plurals
  1. POWeird NSString behavior with formatString
    primarykey
    data
    text
    <p>I ran into a pretty weird issue today. I have this static method (part of a CommonUtilities source file that I've been just created that gathers all the small common methods I'd like to access anywhere in my code, like I usually do btw...)</p> <p>I just want to convert a number into its scientific value using the international system notation (k, M, G, etc.)</p> <p>Here is the code:</p> <pre><code>+ (NSString*)scientificFormatedStringForValue:(NSNumber*)value andUnits:(NSString*)_units { NSMutableString* retStr = [NSMutableString string]; long long longValue = [value longLongValue]; if (longValue &gt; 1000000000) { [retStr appendFormat:@"%d Md%@", longValue / 1000000000, _units]; } else if (longValue &gt; 1000000) { [retStr appendFormat:@"%d M%@", longValue / 1000000, _units]; } else if (longValue &gt; 1000) { [retStr appendFormat:@"%d k%@", longValue / 1000, _units]; } else { [retStr appendFormat:@"%d %@", longValue, _units]; } return retStr; } </code></pre> <p>This is pretty easy right? Ok, here's the deal: the <code>_units</code> is not converted properly.</p> <p>In my example, I use this: </p> <pre><code>[CommonUtilities scientificFormatedStringForValue:[NSNumber numberWithLongLong:longValue] andUnits:@"€"]; </code></pre> <p>I get <code>(null)</code> as <code>_units</code> for the formatted string. If I print the value of <code>_units</code>, it's a dot. So, to try to debug this, I just replaced:</p> <pre><code>[retStr appendFormat:@"%d M%@", longValue / 1000000, _units]; </code></pre> <p>with</p> <pre><code>[retStr appendFormat:@"%d M%@", longValue / 1000000, @"€"]; </code></pre> <p>Still doesn't work. It tried passing a single character (thinking the maybe the € must be converted to some UTF8 stuff or whatever. So I changed the calling method to :</p> <pre><code>[CommonUtilities scientificFormatedStringForValue:[NSNumber numberWithLongLong:longValue] andUnits:@"e"]; </code></pre> <p>Still crappy stuff. I even changed @"€" to [NSString stringWithString:@"€"], but still the same output! I can't figure out what's wrong here, I'm stuck. </p> <p>I thought of a problem in the encoding of the source file, so I deleted it and recreated it, but still the same issue....</p> <p>If any one has even the tiniest clue, that would be most helpful. Thank you guys...</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.
 

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