0

これは、Webサービス「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

これをチェックして

float floatVal = 2.6111;
long roundedVal = lroundf(floatVal);
NSLog(@"%ld",roundedVal);
于 2016-09-07T10:44:24.313 に答える
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);

したがって、どちらかceilまたはfloorメソッドを使用する場合です。

編集:すべてのダブルスに必要なので:

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

私はそれをテストし、すべての桁で動作するコードの下で試してください、

 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

NSNumberFormatter を使用して 10 進数値を丸めることができます。

    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 に答える
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 に答える