OC代码:
@implementation NSString (Float)
(NSString )formatFloat
{
NSString str = [NSString stringWithFormat:@"%.2f",self.floatValue];
return str;
}
@end
JS代码:
require("NSString, NSString, NSString,NSString+Float");
defineClass("NSString", {
formatFloat: function() {
var str = NSString.stringWithFormat("%@",self.floatValue().toFixed(2));
return str;
}
});
总结:
1.扩展类的写法:
如:NSString (Float) 写成defineClass("NSString"),require("NSString+Float")
2.JS的格式化字符串必须用%@
3.js保留两位小数的写法:小数后加.toFixed(2),2为保留位数