如何舍入 NSNumber 并在对象 C 中创建一系列数字

发布于 2024-11-30 01:42:02 字数 909 浏览 4 评论 0原文

我可以知道如何对对象 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.

enter image description here
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡の花香 2024-12-07 01:42:02

查看 math.h 中的数学函数,那里有一些不错的东西,例如

extern double round ( double );
extern float roundf ( float );

至于问题的其余部分,您可能必须将字符串解析为数字,对它们执行您想要的任何操作(舍入、排序等),然后将它们放入回到字符串中。

Check out the math functions in math.h, there's some nice stuff there like

extern double round ( double );
extern float roundf ( float );

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.

り繁华旳梦境 2024-12-07 01:42:02

您的编辑现在更有意义了。这是一个在 NSNumberNSArray 中获取从 0 到最大值的所有偶数的示例(假设所有值都是正数,可以更改为支持负值)通过查找最小浮点值)。

NSArray *sdArray = [NSArray arrayWithObjects:
                    [NSNumber numberWithFloat:2.1],
                    [NSNumber numberWithFloat:1.3],
                    [NSNumber numberWithFloat:4.7],
                    [NSNumber numberWithFloat:3.1],
                    [NSNumber numberWithFloat:4.8],
                    [NSNumber numberWithFloat:15.1],
                    [NSNumber numberWithFloat:7.2],
                    nil];

//Get max value using KVC
float fmax = [[sdArray valueForKeyPath:@"@max.floatValue"] floatValue];

//Ceiling the max value
int imax = (int)ceilf(fmax);

//Odd check to make even by checking right most bit
imax = (imax & 0x1) ? imax + 1 : imax;

NSMutableArray *array = [NSMutableArray arrayWithCapacity:(imax / 2) + 1];
//Assuming all numbers are positive 
//(should probably just use unsigned int for imax and i)
for(int i = 0; i <= imax; i +=2)
{
    [array addObject:[NSString stringWithFormat:@"%d", i]];
}

NSLog(@"%@", array);

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 of NSNumbers (assuming all values are positive, could be changed to support negative values by finding minimum float value as well).

NSArray *sdArray = [NSArray arrayWithObjects:
                    [NSNumber numberWithFloat:2.1],
                    [NSNumber numberWithFloat:1.3],
                    [NSNumber numberWithFloat:4.7],
                    [NSNumber numberWithFloat:3.1],
                    [NSNumber numberWithFloat:4.8],
                    [NSNumber numberWithFloat:15.1],
                    [NSNumber numberWithFloat:7.2],
                    nil];

//Get max value using KVC
float fmax = [[sdArray valueForKeyPath:@"@max.floatValue"] floatValue];

//Ceiling the max value
int imax = (int)ceilf(fmax);

//Odd check to make even by checking right most bit
imax = (imax & 0x1) ? imax + 1 : imax;

NSMutableArray *array = [NSMutableArray arrayWithCapacity:(imax / 2) + 1];
//Assuming all numbers are positive 
//(should probably just use unsigned int for imax and i)
for(int i = 0; i <= imax; i +=2)
{
    [array addObject:[NSString stringWithFormat:@"%d", i]];
}

NSLog(@"%@", array);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文