0

我从网络服务 "rateavg": "2.6111" 得到这个

现在我把它放在一个字符串中。如何做到这一点,如果它来了 2.6 它将显示 3 ,如果它来了 2.4 或 2.5 它将显示 2 ?

如何得到这个我没有得到。请帮我

4

9 回答 9

2

尝试这个

float f=2.6;
NSLog(@"%.f",f);

希望这可以帮助。

于 2016-09-07T10:24:48.710 回答
1

我想出了这个,你的查询的副本:

NSString* str = @"2.611";
double duble = [str floatValue];

NSInteger final = 0;


if (duble > 2.5) {
    final = ceil(duble);
}else{
    final = floor(duble);
}

NSLog(@"%ld",(long)final);

因此,这是使用其中一个ceilfloor方法的情况。

编辑:因为你想要所有双打:

NSString* str = @"4.6";
double duble = [str floatValue];

NSInteger final = 0;

NSInteger temp = floor(duble);
double remainder = duble - temp;

if (remainder > 0.5) {
    final = ceil(duble);
}else{
    final = floor(duble);
}

NSLog(@"%ld",(long)final);
于 2016-09-07T10:26:20.903 回答
1

请使用这个

lblHours.text =[NSString stringWithFormat:@"%.02f",  [yourstrvalue doubleValue]];

更新

   NSString *a =@"2.67899";
   NSString *b    =[NSString stringWithFormat:@"%.01f",  [a doubleValue]];
    // b will contane only one vlue after decimal
    NSArray *array = [b componentsSeparatedByString:@"."];

    int yourRating;
    if ([[array lastObject] integerValue] > 5) {

        yourRating = [[array firstObject] intValue]+1;

    }
    else
    {
        yourRating = [[array firstObject] intValue];
    }
    NSLog(@"%d",yourRating);
于 2016-09-07T10:27:00.057 回答
1

检查这个

float floatVal = 2.6111;
long roundedVal = lroundf(floatVal);
NSLog(@"%ld",roundedVal);
于 2016-09-07T10:44:24.313 回答
1

试试下面的代码,我已经测试过它并适用于每个数字,

 NSString *str = @"2.7";

NSArray *arr = [str componentsSeparatedByString:@"."];


NSString *firstDigit = [arr objectAtIndex:0];
NSString *secondDigit = [arr objectAtIndex:1];

if (secondDigit.length > 1) {

    secondDigit = [secondDigit substringFromIndex:1];
}

int secondDigitIntValue = [secondDigit intValue];
int firstDigitIntValue = [firstDigit intValue];

if (secondDigitIntValue > 5) {

    firstDigitIntValue = firstDigitIntValue + 1;
}

NSLog(@"final result : %d",firstDigitIntValue);

或另一种解决方案 - 有点短

 NSString *str1 = @"2.444";

float my = [str1 floatValue];

NSString *resultString = [NSString stringWithFormat:@"%.f",my];  // if want result in string

NSLog(@"%@",resultString);

int resultInInt = [resultString intValue]; //if want result in integer
于 2016-09-07T10:48:52.683 回答
0

要将值四舍五入到最接近的整数,请使用roundf()的函数math

先导入math.h

#import "math.h"

例子,

float ValueToRoundPositive;

ValueToRoundPositive = 8.4;

int RoundedValue = (int)roundf(ValueToRoundPositive); //Output: 8

NSLog(@"roundf(%f) = %d", ValueToRoundPositive, RoundedValue);


float ValueToRoundNegative;

ValueToRoundNegative = -6.49;

int RoundedValueNegative = (int)roundf(ValueToRoundNegative); //Output: -6

NSLog(@"roundf(%f) = %d", ValueToRoundNegative, RoundedValueNegative);

在此处阅读文档以获取更多信息:

http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/roundf.3.html

于 2016-09-07T11:02:26.850 回答
0
    NSString *value = @"1.23456";
    float floatvalue = value.floatValue;
    int rounded = roundf(floatvalue);
    NSLog(@"%d",rounded);

如果你哪轮价值更大,请使用ceil(floatvalue)

如果你是价值较小的一轮,请使用floor(floatvalue)

于 2016-09-07T11:05:33.017 回答
0

您可以使用 NSNumberFormatter 对十进制值进行四舍五入。您可以查看一些示例:

    NSNumberFormatter *format = [[NSNumberFormatter alloc] init];
    [format setPositiveFormat:@"0.##"];
    NSLog(@"%@", [format stringFromNumber:[NSNumber numberWithFloat:25.342]]);
    NSLog(@"%@", [format stringFromNumber:[NSNumber numberWithFloat:25.3]]);
    NSLog(@"%@", [format stringFromNumber:[NSNumber numberWithFloat:25.0]]);

对应结果:

2010-08-22 15:04:10.614 a.out[6954:903] 25.34
2010-08-22 15:04:10.616 a.out[6954:903] 25.3
2010-08-22 15:04:10.617 a.out[6954:903] 25
于 2016-09-07T11:12:44.177 回答
0
NSString* str = @"2.61111111";
double value = [str doubleValue];

2.5 -> 3: int num = value+0.5;
2.6 -> 3: int num = value+0.4;

根据需要设置:

double factor = 0.4
if (value < 0) value *= -1;
int num = value+factor;

NSLog(@"%d",num);
于 2016-09-07T15:22:14.890 回答