简体   繁体   English

在iOS 7.1应用程序Xcode 5.1中获取数字百分比时遇到问题

[英]Having issues getting a percentage of a number in iOS 7.1 applicaion xcode 5.1

In my iOS 7.1 application that I'm trying to develop, I need to figure out percentages of a specific number which is entered in a UITextField. 在我尝试开发的iOS 7.1应用程序中,我需要找出在UITextField中输入的特定数字的百分比。 It would appear when executing the code I get the wrong percentage of that number entered. 当执行代码时,输​​入的那个数字的百分比错误,它将出现。

I've tried two different ways to get the require percentage answer I'm looking for, however it keeps giving the wrong answer. 我尝试了两种不同的方法来获取所需的百分比要求答案,但是它总是给出错误的答案。

Below here is the two methods that I've tried. 下面是我尝试过的两种方法。

For example I want to get 72% of 250. If you were to do this on a calculator or better yet a excel spreadsheet I get the right answer 250 x 1 - 72% = 70. This is the correct answer I want 例如,我想要得到250%的72%。如果您要在计算器上或更佳的Excel电子表格中执行此操作,我会得到正确的答案250 x 1-72%=70。这是我想要的正确答案

Method1 (.m file) Not working 方法1(.m文件)不起作用

Values that are set the the specific .text parameters: 设置特定.text参数的值:

Entered in the UITextField _linuxOracleOnDiskw_oRTC.text = 250
Value set to UITextField _formulaNumber.text = 1
Percentage Value set to _linuxOracle_Percent.text = 0.72 

_linuxOracleOnDiskwithRTC.text = [NSString stringWithFormat:@"%.2f", ([_linuxOracleOnDiskw_oRTC.text doubleValue])*([_formulaNumber.text doubleValue])-([_linuxOracle_Percent.text doubleValue])];

When executed I the answer or vale that gets entered in the UITextField _linuxOracleOnDiskwithRTC.text is 249.28. 执行后,在UITextField _linuxOracleOnDiskwithRTC.text中输入的答案或谷值是249.28。 This is wrong should be 70 这应该是70错误

Second method tried is as follows: 尝试的第二种方法如下:

float linuxOracleOnDiskw_oRTC = [_linuxOracleOnDiskw_oRTC.text floatValue];

_linuxOracleOnDiskwithRTC.text = [NSString stringWithFormat:@"%.2f", (linuxOracleOnDiskw_oRTC * 1 - 72/100.0f )]; _linuxOracleOnDiskwithRTC.text = [NSString stringWithFormat:@“%。2f”,(linuxOracleOnDiskw_oRTC * 1-72 / 100.0f)];

If someone can tell me what I maybe doing wrong and point me in the right direction with calculating percentages of a specific number entered in a UITextField I would be extremely GREATFUL. 如果有人可以告诉我我可能做错了什么,并通过计算在UITextField中输入的特定数字的百分比来指出正确的方向,那我将非常高兴。

Don't omit your brackets when performing calculations with code. 使用代码执行计算时,请不要省略括号。 For "250 x 1 - 72%" to get 70, you need to do this: 为了使“ 250 x 1-72%”获得70,您需要执行以下操作:

250 x (1 - 0.72) = 250 x 0.28 = 70 250 x(1-0.72)= 250 x 0.28 = 70

Formulas in () will be calculated first, followed by multiplication and division (whichever first), then followed by addition and subtraction (whichever first). 将首先计算()中的公式,然后是乘法和除法(先取其中的一个),然后是加法和减法(先取哪个)。 So, insert your brackets appropriately. 因此,适当插入括号。

Your formula to calculate the percentage is incorrect. 您计算百分比的公式不正确。 One correct way to calculate percentages is 一种正确的百分比计算方法是

250 / 100 x 72 250/100 x 72

This gives you 72% of 250. The result of that isn't 70, btw, but 180. If you want 70, then you don't want 72% but 28% (or 100% - 72%): 这将为您提供250的72%。结果不是70,顺便说一句,而是180。如果您想要70,那么您就不需要72%而是28%(或100%-72%):

250 / 100 x (100 - 72) 250/100 x(100-72)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM