Pages

August 8, 2013

CGFloat to NSString with custom decimal separator

A convenient shortcut of [NSString stringWithFormat:] for a CGFloat number with custom decimal separator.

/** Convert a float to its string representation.

 @param n Floating point to convert.
 @param separator String to use as separator for units and decimals.
 @return Returns the string value of the number.
 @see stringFromInt:
 @see stringFromBool:ofType:
 @see floatFromString:withSeparatorForDecimal:

 */
+ (NSString *)stringFromFloat:(CGFloat)n usingSeparatorForDecimal:(NSString *)separator {
    NSString *temp = [NSString stringWithFormat:@"%f", n];
    return [temp stringByReplacingOccurrencesOfString:@"." withString:separator];
}


Download the ready-for-use source file (.m) of CGFloat to NSString with custom decimal separator

No comments:

Post a Comment