GRMustache格式化数字,或带有iOS格式数字格式的HTML模板引擎 [英] GRMustache formatted numbers, or an HTML Template Engine with Number Formatting in iOS

查看:427
本文介绍了GRMustache格式化数字,或带有iOS格式数字格式的HTML模板引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何最好地解决这个问题。我试着用自己的方式解决这个问题,但到目前为止失败 。我尝试过使用GRMustache,但只是意识到我正试图显示 float ,这在我尝试使用的模板中看起来很丑。



基本上,我有一个模型,我试图通过模板将输出作为HTML输出。理想情况下,我只是将变量名称/ keypath放入模板中,并且模板只是用实际值(几乎)进行了解析。但是,我使用的模型使用了所有计算的浮点数,我真的很喜欢它们以逗号分隔的整数字符串呈现(例如(float)9382.233325 => 9,382)。



我似乎无法在GRMustache中找到涵盖这种情况的任何文档,但我想这不是一个不常见的要求。有人知道如何用GRMustache或其他技术来做到这一点吗?

我是GRMustache的作者。 p>

在GRMustache中没有,也不会有任何浮点格式化功能,因为在操作系统中已经有一个非常适合的工具:NSNumberFormatter。



由于您向GRMustache提供模型对象,因此我的建议是:

在您的模型中声明一个类别,然后添加每个你的格式化值的具体方法:

$ $ p $ @interface MYModel(GRMustache)
// name会匹配你的原始值属性名称
- (NSString *)formattedValue;
@end

在实现文件中,使用NSNumberFormatter:

  @implementation MYModel(GRMustache)
- (NSString *)formattedValue
{
//检查NSNumberFormatter参考初始化
//所需输出的NSSNumberFormatter。
NSNumberFormatter * formatter = [NSSNumberFormatter ...];
return [formatter stringFromNumber:[self value]];
}
@end

当心创建许多NSNumberFormatter实例可能代价高昂。一个好的做法是提供一个返回共享方法的共享方法。上面的代码只是该技术的一个提示。

最后,在您的模板中,替换 {{value}} {{formattedValue}}



Happy GRMustache!


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 just look hideous in the template I'm trying to use.

Basically, I have a model that I'm trying to have output as HTML via a template. Ideally, I just put the variable names/keypaths into the template and the template just gets parsed with the actual values (pretty much) rendered in place. But, the model I'm using uses floats for all its calculations and I'd really like them rendered as comma-separated integer strings (e.g. (float)9382.233325 => "9,382").

I can't seem to find any documentation in GRMustache that covers a situation like this, but I imagine this can't be an uncommon requirement. Does anyone know how to do this with GRMustache or through some other technique?

解决方案

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!

这篇关于GRMustache格式化数字,或带有iOS格式数字格式的HTML模板引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆