如何在Objective-C中格式化计算的最终结果数字



我有这个代码:

double s = [year.text doubleValue]*31536000;
second.text=[[NSString alloc] initWithFormat:@"%2g", s]; 

当我尝试计算时,最终结果是3.1536e+07

如何在没有e+07的情况下使结果31536000

如果

指数小于 -4 或大于或等于精度,则%g%G转换分别以 %e%E 的样式打印参数;否则,它们使用 %f 样式。
您应该使用 %f .

double s = [year.text doubleValue]*31536000;
second.text=[[NSString alloc] initWithFormat:@"%f", s];

简单使用 %f。

NSString *str = [[NSString alloc] initWithFormat:@"%f", s];
second.text = str;

second可能会导致字符串没有"e+07"

f是因为我们正在使用浮点数。

你应该尝试:

double s = [year.text doubleValue]*31536000;
second.text=[[NSString alloc] initWithFormat:@"%lf", s]; 

最新更新