How to add a decimal button so I don't have to do whole numbers? If i wanted any decimal number like 1.2 or 100.4 or 3.0 or anything like that, how would I add it to the calculator I'm making?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You're not giving me much information in your question. How do the other buttons work?
If I was making a calculator I would have a label at the top showing the current reading. On press of a number button I would update the label with the number at the end.
For a decimal button you just add a . to the end of the label. You might want to have a global variable BOOL hasDecimalPlace
and set it to true so you know if there is already a decimal place. Just remember to set it to false again when you clear the view or do a calculation or similar.
回答2:
- (IBAction)Decimal:(id)sender{
NSString *currentText = Text.text;
if ([currentText rangeOfString:@"." options:NSBackwardsSearch].length == 0) {
Text.text = [Text.text stringByAppendingString:@"."];
}
}