Easiest way to format a number with thousand separators to an NSString according to the Locale

For 10.6 this works: NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setFormatterBehavior: NSNumberFormatterBehavior10_4]; [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle]; NSString *numberString = [numberFormatter stringFromNumber: [NSNumber numberWithInteger: i]]; And it properly handles localization.

Leaving values blank if not passed in str.format

You can follow the recommendation in PEP 3101 and use a subclass Formatter: import string class BlankFormatter(string.Formatter): def __init__(self, default=””): self.default=default def get_value(self, key, args, kwds): if isinstance(key, str): return kwds.get(key, self.default) else: return string.Formatter.get_value(key, args, kwds) kwargs = {“name”: “mark”, “adj”: “mad”} fmt=BlankFormatter() print fmt.format(“My name is {name} and I’m really {adj}.”, **kwargs) # … Read more

How to produce “human readable” strings to represent a TimeSpan

To get rid of the complex if and switch constructs you can use a Dictionary lookup for the correct format string based on TotalSeconds and a CustomFormatter to format the supplied Timespan accordingly. public string GetReadableTimespan(TimeSpan ts) { // formats and its cutoffs based on totalseconds var cutoff = new SortedList<long, string> { {59, “{3:S}” … Read more

Using an array as argument for string.Format()

You can convert int array to string array as pass it using System.Linq Select() extension method. infoText.text = string.Format(“Player1: {0} \nPlayer2: {1} \nPlayer3: {2} \nPlayer4: {3}”, place.Select(x => x.ToString()).ToArray()); Edit: In C# 6 and above, you can also able to use String Interpolation instead of using string.Format() infoText.text = $”Player1: {place[0]}\nPlayer2: {place[1]} \nPlayer3: {place[2]} \nPlayer4: … Read more

Ruby formatting for ordinals: ‘1’ as ‘1st’, ‘2’ as ‘2nd’ etc

Looks like you are looking for ordinalize: The Ruby on Rails framework is chock full of interesting little nuggets. Ordinalize is a number extension that returns the corresponding ordinal number as a string. For instance, 1.ordinalize returns “1st” and 22.ordinalize return “22nd”. Example: place = 3 puts “You are currently in #{place.ordinalize} place.” Result: You … Read more

How to format a float with a maximum number of decimal places and without extra zero padding?

What you’re asking for should be addressed by rounding methods like the built-in round function. Then let the float number be naturally displayed with its string representation. >>> round(65.53, 4) # num decimal <= precision, do nothing ‘65.53’ >>> round(40.355435, 4) # num decimal > precision, round ‘40.3554’ >>> round(0, 4) # note: converts int … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)