Skip to content Skip to sidebar Skip to footer

GRMustache Formatted Numbers, Or An HTML Template Engine With Number Formatting In IOS

I'm not sure how to best go about this. I've tried solving this my own way, but failed so far. I tried using GRMustache, only to realize that I'm trying to display floats that ju

Solution 1:

I'm the author of GRMustache.

There isn't, and there will never be any float formatting features in GRMustache, because there is already a perfectly well suited tool in the OS: NSNumberFormatter.

Since you're giving to GRMustache your model objects, here is my advice:

Declare a category on your model, and add a specific method for each of your formatted value:

@interface MYModel(GRMustache)
// name would match your original value property name
- (NSString *)formattedValue;
@end

In the implementation file, use a NSNumberFormatter:

@implementation MYModel(GRMustache)
- (NSString *)formattedValue
{
  // Check the NSNumberFormatter reference for initializing
  // the NSSNumberFormatter for your desired output.
  NSNumberFormatter *formatter = [NSSNumberFormatter ...]; 
  return [formatter stringFromNumber: [self value]];
}
@end

Beware creating many NSNumberFormatter instances may be costly. A good practice is to provide a shared method that returns a shared one. The code above is just a hint for the technique.

Finally, in your template, replace {{value}} tags with {{formattedValue}}.

Happy GRMustache!


Solution 2:

GRMustache 1.12 now features a better API for number formatting: https://github.com/groue/GRMustache/blob/master/Guides/sample_code/number_formatting.md


Post a Comment for "GRMustache Formatted Numbers, Or An HTML Template Engine With Number Formatting In IOS"