如何舍入 NSNumber 并在对象 C 中创建一系列数字
我可以知道如何对对象 C 中的 NSNumber 进行四舍五入吗?
for(int i=6; i>=0; i--)
{
DayOfDrinks *drinksOnDay = [appDelegate.drinksOnDayArray objectAtIndex:i];
NSString * dayString= [NSDate stringForDisplayFromDateForChart:drinksOnDay.dateConsumed];
[dayArray addObject:dayString];//X label for graph the day of drink.
drinksOnDay.isDetailViewHydrated = NO;
[drinksOnDay hydrateDetailViewData];
NSNumber *sdNumber = drinksOnDay.standardDrinks;
[sdArray addObject: sdNumber];
}
这个 sdArray 里面都是这样的数字 2.1, 1.3, 4.7, 3.1, 4.8, 15.1, 7.2;
我需要绘制一个图表 Y 轴,所以我需要一个来自 NSNumber 的字符串来显示 这个 {@"0", @"2", @"4", @"6", @"8", @"10", @"12",@"14",@"16"} 的 NSString 。因为我需要从零开始,所以我需要确定哪个是最大的数值。在本例中,图表中显示 16 时将为 15.1。我不想做静态标签,而是想做动态标签。
您在图中看到的是静态编号而不是动态编号。
很抱歉错过了重要信息。
感谢所有的评论
May i know how to round up a NSNumber in object C ?
for(int i=6; i>=0; i--)
{
DayOfDrinks *drinksOnDay = [appDelegate.drinksOnDayArray objectAtIndex:i];
NSString * dayString= [NSDate stringForDisplayFromDateForChart:drinksOnDay.dateConsumed];
[dayArray addObject:dayString];//X label for graph the day of drink.
drinksOnDay.isDetailViewHydrated = NO;
[drinksOnDay hydrateDetailViewData];
NSNumber *sdNumber = drinksOnDay.standardDrinks;
[sdArray addObject: sdNumber];
}
inside this sdArray are all the numbers like this 2.1, 1.3, 4.7, 3.1, 4.8, 15.1, 7.2;
i need to plot a graph Y axis so i need a string of whatever is from the NSNumber to show
a NSString of this {@"0", @"2", @"4", @"6", @"8", @"10", @"12",@"14",@"16"}. as i need to start from zero, i need to determine which is the biggest number value. in this case it will be 15.1 to show 16 in the graph. instead of doing a static labeling, i will like to do a dynamic labeling.
what you have seen in the graph is static numbering not dynamic.
i'm sorry for missing out the important info.
thanks for all the comments
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 math.h 中的数学函数,那里有一些不错的东西,例如
至于问题的其余部分,您可能必须将字符串解析为数字,对它们执行您想要的任何操作(舍入、排序等),然后将它们放入回到字符串中。
Check out the math functions in math.h, there's some nice stuff there like
As for the rest of your question you probably have to parse your strings into numbers, perform whatever action you want on them (rounding, sorting, etc) then put them back into strings.
您的编辑现在更有意义了。这是一个在
NSNumber
的NSArray
中获取从 0 到最大值的所有偶数的示例(假设所有值都是正数,可以更改为支持负值)通过查找最小浮点值)。Your edit now makes a lot more sense. Here is an example of getting all even numbers from your 0 to your max value in your
NSArray
ofNSNumber
s (assuming all values are positive, could be changed to support negative values by finding minimum float value as well).