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;
}
});
總結(jié):
1.擴(kuò)展類(lèi)的寫(xiě)法:
如:NSString (Float) 寫(xiě)成defineClass("NSString")讲岁,require("NSString+Float")
2.JS的格式化字符串必須用%@
3.js保留兩位小數(shù)的寫(xiě)法:小數(shù)后加.toFixed(2)挤茄,2為保留位數(shù)