2011-01-29 37 views
0

我現在得到了小數工作,但由於某種原因它顯示倒退。這也可能是我缺乏數學技能,但這裏是代碼:小數位在Xcode中顯示不正確?

-(IBAction)buttonDigitPressed:(id)sender { 
    currentNumber = (float)[sender tag] + currentNumber /10; 
    priceOne.text = [NSString stringWithFormat:@"%.2f",currentNumber]; 
} 

現在,當我在這個順序我的自定義鍵盤上鍵入:5-2-1,它向後顯示數字:1.25。我希望它顯示5.21。有任何想法嗎?

+1

第二次嘗試?你的意思是你之前曾問過這個問題?如果您要刪除問題再次提問,請不要再次提問。 http://meta.stackexchange.com/questions/74459/user-systematically-deleting-and-re-posting-questions/ – 2011-01-29 14:30:03

+0

這不是同一個問題,我的意思是第二次嘗試詢問這個特定主題。 – 2011-01-29 14:31:59

回答

1

在你@interface添加一個新的實例變量

float currentMultiplier; 

設置它在你(重新)初始化程序,所以:

currentMultiplier = 1.0; 

,並重新編寫你的問題,如上面所提到的常規:

-(IBAction)buttonDigitPressed:(id)sender 
{ 
    currentNumber += (float)[sender tag]*currentMultiplier; 
    currentMultiplier *= 0.1; 
    priceOne.text = [NSString stringWithFormat:@"%.2f",currentNumber]; 
} 
1
-(IBAction)buttonDigitPressed:(id)sender { 

    currentNumber = currentNumber*10 + (float)[sender tag]; 

    calculatorScreen.text = [NSString stringWithFormat:@"%2f",currentNumber]; 

}