1
我需要有人鍵入一個值爲「40」,然後該應用程序將其轉換爲另一個值除以7.5。目前,我已經做了:NSInteger是否允許小數位?
NSString *pCO2 = _pCO2.text;
float thepCO2 = [pCO2 floatValue];
NSInteger newpCO2 = thepCO2/7.5;
NSString *finalpCO2 = [NSString stringWithFormat:@"%d", newpCO2];
,再後來,另一種觀點認爲,應用程序拿起的NSString稱爲finalpCO2和像這樣將其轉換回數字:
float newpCO2 = ([pCO2 floatValue]);
然而,當我這樣做時,對於初始pCO2,38以上的值似乎被四捨五入爲5(38除以7.5 = 5.0666),並且低於此值被四捨五入爲4(37除以7.5 = 4.93)
有誰知道爲什麼這可能會發生?我希望這個數字至少到最接近的小數位。
感謝
Layth
那麼我該如何改變這一點?我可以使用'NSFloat newpCO2 = thepCO2/7.5; NSString * finalpCO2 = [NSString stringWithFormat:@「%d」,newpCO2];'現在能工作嗎? – LaythT
沒有'%d'是整數。改用'%f'。請參閱https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1 – shf301
@ shf301我沒有看到'NSFloat'。雖然有一個'CGFLoat'。 – Steven