[英]i want to use integer and float in objective-c for a same function?
I'm making an calculator in xcode using Objective-C i used float in syntax but i want that when answer is float calculator show float type not integer only and i want that when answer is integer it show integer when answer is float it shows float. 我正在使用Objective-C在xcode中制作一个计算器我在语法中使用了float但我希望当回答是浮点计算器时显示float类型不仅仅是整数而且我希望当answer是整数时它会显示整数,当answer是float时它会显示float 。 here is code https://github.com/syntaxxerrorr/Calculator
这是代码https://github.com/syntaxxerrorr/Calculator
Test whether the value is int or not. 测试值是否为int。 You can do it like this:
你可以这样做:
if (fmod(fVal, 1.0) == 0.0) // is integer
Get integer result: 获取整数结果:
int result = (int)roundf(myFloat );
Display the corresponding value, using the condition. 使用条件显示相应的值。
For example: 例如:
float answer = 12.0;
float truncAnswer;
int intAnswer;
truncAnswer = truncf(answer);
intAnswer = (int)truncAnswer;
if(answer == truncAnswer)
{
NSLog(@"Integer Answer = %d", intAnswer);
}else{
NSLog(@"Float Answer = %f", answer);
}
If you change the initialization of answer to 12.0, it will print the integer answer. 如果将answer的初始化更改为12.0,则会打印整数答案。
Do you know NSNumber
in Objective-C is a class represent for Float, Double, Int, Integer, etc? 你知道Objective-C中的
NSNumber
是Float,Double,Int,Integer等的类吗? So you can return NSNumber in your function. 因此,您可以在函数中返回NSNumber。
Let's try to use it. 我们试着用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.