我想知道如何评估方程的字符串表示,就好像它是一个真实的方程:
if(@"15+14==23")
{
//True statement...
}
else
{
//False statement....
}
Run Code Online (Sandbox Code Playgroud)
我想返回"假",因为15 + 14不等于23.我怎么能让它工作?
cocoa cocoa-touch objective-c expression-evaluation nsstring
诺布在这里想要计算iPhone的复利.
float principal;
float rate;
int compoundPerYear;
int years;
float amount;
Run Code Online (Sandbox Code Playgroud)
公式应为:amount = principal*(1 + rate/compoundPerYear)^(rate*years)
我得到的答案略有错误:
amount = principal*pow((1+(rate/compoundPerYear)), (compoundPerYear*years));
Run Code Online (Sandbox Code Playgroud)
我正在以.1的速率测试它,但调试器报告.100000001.
我做错了吗?我应该使用双打还是特殊课程(例如,NSNumber)?
感谢您的任何其他想法!
经过进一步的研究,似乎NSDecimalNumber类可能正是我所需要的.现在我只需要弄清楚如何使用这个坏男孩.
可能重复:
什么是快速C或Objective-C数学解析器?
我有一个创建一个函数,它将公式作为NSString.我必须在String中替换一些东西并在执行公式后获取值.我怎样才能做到这一点...
例如:NSString*formulaString = @"((A/B)-1)*100";
现在我可以将A替换为20,将B替换为10.但是如何将计算值((20/10)-1)*100替换为100
谢谢,本
我想转换一个字符串例如
NSString *stringWithNumber = @"3/4"
Run Code Online (Sandbox Code Playgroud)
进入NSNumber.这怎么可能?
objective-c ×4
ios ×2
cocoa ×1
cocoa-touch ×1
double ×1
finance ×1
iphone ×1
math ×1
nsstring ×1