最近在做分时K线的项目,牵涉到刻度轴的时候,产品要求根据昨日的收盘价进行小数位的保留,不足补零。
效果图如下:debu代码
NSLog(@"%@",[ViewController notRounding:@(0.1246) afterPoint:1]);
NSLog(@"%@",[ViewController notRounding:@(0.1246) afterPoint:2]);
NSLog(@"%@",[ViewController notRounding:@(0.1246) afterPoint:3]);
NSLog(@"%@",[ViewController notRounding:@(0.1246) afterPoint:4]);
NSLog(@"%@",[ViewController notRounding:@(0.1246) afterPoint:5]);
核心代码逻辑
+ (NSString *)notRounding:(id)price
afterPoint:(NSInteger)position {
//生成format格式
NSString *format = [NSString stringWithFormat:@"%%.%ldf",(long)position];
CGFloat value = 0.;
//string 和 number 兼容
if ([price respondsToSelector:@selector(doubleValue)]) {
value = [price doubleValue];
}
NSString *number = [NSString stringWithFormat:format,value];
return number;
}