如何将这个值转换成绝对值?

How to convert this value into absolute value?

我是从网络服务获取的 "rateavg": "2.6111"

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

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

试试这个

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

希望对您有所帮助。

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

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);

请使用这个

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);

检查这个

float floatVal = 2.6111;
long roundedVal = lroundf(floatVal);
NSLog(@"%ld",roundedVal);

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

 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

要将值舍入到最接近的整数,请使用 mathroundf() 函数。

首先导入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

    NSString *value = @"1.23456";
    float floatvalue = value.floatValue;
    int rounded = roundf(floatvalue);
    NSLog(@"%d",rounded);

如果你的回合有更大的价值请使用ceil(floatvalue)

如果你想要价值较小的回合,请使用floor(floatvalue)

您可以使用 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
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);