If value of cell is positive do equation else do another equation - google-sheets

I'm trying to figure out how to pull a number from a cell and check if it's positive or negative and then from there do one of 2 equations to produce a final number. This is my code, but it doesn't work because the equations become strings. Not sure if there is a simple solution or if it requires a script of some sort since Google Sheets doesn't have an EVALUATE function.
=(CONCATENATE(IF(AC2>=0,"100 / (AC2 + 100) * 100","(AC2*-1)/ ((AC2*-1)+100) * 100)"),"%"))
Edit:
Final Code:
=CONCATENATE(IF(AC2>=0,ROUND(100/(AC2+100)*100, 0),ROUND((AC2*-1)/((AC2*-1)+100)*100, 0)),"%")

Remove the quotes around your equations so it looks more like..
=IF(AC2>=0,100/(AC2+100)*100,(AC2*-1)/((AC2*-1)+100)*100)
Any reason you are concatenating a %? Perhaps you just want to format it as a percentage.

Related

Why multiply two double in dart result in very strange number

Can anyone explain why the result is 252.99999999999997 and not 253? What should be used instead to get 253?
double x = 2.11;
double y = 0.42;
print(((x + y) * 100)); // print 252.99999999999997
I am basically trying to convert a currency value with 2 decimal (ie £2.11) into pence/cent (ie 211p)
Thanks
In short: Because many fractional double values are not precise, and adding imprecise values can give even more imprecise results. That's an inherent property of IEEE-754 floating point numbers, which is what Dart (and most other languages and the CPUs running them) are working with.
Neither of the rational numbers 2.11 and 0.42 are precisely representable as a double value. When you write 2.11 as source code, the meaning of that is the actual double values that is closest to the mathematical number 2.11.
The value of 2.11 is precisely 2.109999999999999875655021241982467472553253173828125.
The value of 0.42 is precisely 0.419999999999999984456877655247808434069156646728515625.
As you can see, both are slightly smaller than the value you intended.
Then you add those two values, which gives the precise double result 2.529999999999999804600747665972448885440826416015625. This loses a few of the last digits of the 0.42 to rounding, and since both were already smaller than 2.11 and 0.42, the result is now even more smaller than 2.53.
Finally you multiply that by 100, which gives the precise result 252.999999999999971578290569595992565155029296875.
This is different from the double value 253.0.
The double.toString method doesn't return a string of the exact value, but it does return different strings for different values, and since the value is different from 253.0, it must return a different string. It then returns a string of the shortest number which is still closer to the result than to the next adjacent double value, and that is the string you see.

Google sheets recursive ROUND for fractional parts

I have a number with fractional parts, but Google sheets ROUND function works only for first number specified in places parameter:
1.48852 => ROUND(1.48852) => 1
1.48852 => ROUND(ROUND(ROUND(ROUND(ROUND(ROUND(1.48852,5),4),3),2),1),0) => 2
Is any function in Google sheets that would work as shown in the second case?
Basically, I want something like ROUND but work recursively.
To answer your question directly, no, I don't believe that there is any function that enables recursive rounding (or recursive anything for that matter).
However, with the same degree of curiosity that you showed in asking this question, I've looked at how I might achieve a recursive result through scripting. This is the result.
function reround(input) {
var num1 = 5;
do {
input = input.toFixed(num1);
input=+input;
num1--;
}while (num1 >-1); // continue running the loop while is greater than -1, i.e. until num1 = 0
return input;
}
This code gets entered as a script and then the function is available for use in the spreadsheet. It only takes one parameter (value) because the number of places is coded into the script. But no doubt, one could modify the code to accept the number of decimal places as a parameter also (I wasn't that curious;).
This is the sequence of values at each stage of the conventional and recursive rounding.
This is the log of values from the REROUND process.
Interesting to note the difference at three decimal places. It's not something that I've explored, so I don't have an explanation.

Lua random number to the 8th decimal place

How do I get a random number in Lua to the eighth decimal?
Example : 0.00000001
I have tried the following and several variations of this but can not get the format i need.
math.randomseed( os.time() )
x = math.random(10000000,20000000) * 0.00000001
print(x)
i would like to put in say 200 and get this 0.00000200
Just grab a random number from 0-9, and slide it down 6 places. You can use format specifiers to create the string representation of the number that you desire. For floats we use %f, and indicate how many decimal places we want to have with an intermediate .n, where n is a number.
math.randomseed(os.time())
-- random(9) to exclude 0
print(('%.8f'):format(math.random(0, 9) * 1e-6))
--> '0.00000400'
string.format("%.8f",math.random())
to help anyone else. my question should have been worded a bit better. i wanted to be able to get random numbers and get it to the 8th decimal place.
but i wanted to be able to have those numbers from 1-10,000 so he is updated how i wanted it and the help of Oka got me to this
math.randomseed(os.time())
lowest = 1
highest = 7000
rand=('%.8f'):format(math.random(lowest, highest) / 100000000)
print(rand)
Hope this helps someone else or if it can be cleaned up please let me know

Computing UILabel height & UIFont height (for number of lines) using ceil() or roundf()?

I have this values that i've logged:
label.frame.size.height :18.000000, label.font.lineHeight: 17.895000
if i use roundf() like:
roundf(label.frame.size.height / label.font.lineHeight) // answer: 1
while with ceil()
ceil(label.frame.size.height / label.font.lineHeight) // answer: 2
but when computed manually: answer is 1.00586756
I wonder whats the best and more reliable(generally) between this two. Why is everybody using ceil() to determine the number of lines of UILabel?
In the case of number of lines each letter after the limit a line could display should be taken to next line so .005 is also significant this .005 part of the text should carry to next line. So it is better to use ceil() rather than roundf( ). In roundf( ) a value will be significant only when it is greater or equal to its half value)
ceil()
The C library function ceil(x) returns the smallest integer value greater than or equal to x.
I still dont understand why must of the people use ceil() when computing the number of line since roundf() is more accurate..
But when talking about computing for the number of line.. i look to me that 'roundf()' is indeed more accurate, but since its number of lines.. decimal values are not significant..
Computing what is the image:
54 / 17.895000 = 3.01760268
And numberOflines = 3
if we use roundf() answer would be 3 as well
while if ceil() is already 4
therefore using floor() or simply converting the result to int will do the work:
int result = (int)floor(answer);
//or
int result = (int)answer;
About my question, i think roundf() to the work for me for computing number of lines generally..
I'm making a class that will compute the number of line base from this values, and will be used by the whole app.

Small numbers in Objective C 2.0

I created a calculator class that does basic +,-, %, * and sin, cos, tan, sqrt and other math functions.
I have all the variables of type double, everything is working fine for big numbers, so I can calculate numbers like 1.35E122, but the problem is with extremely small numbers. For example if I do calculation 1/98556321 I get 0 where I would like to get something 1.01464E-8.
Should I rewrite my code so that I only manipulate NSDecimalNumber's and if so, what do I do with sin and cos math functions that accept only double and long double values.
1/98556321
This division gives you 0 because integer division is performed here - the result is an integer part of division. The following line should give you floating point result:
1/(double)98556321
integer/integer is always an integer
So either you convert the upper or the lower number to decimal
(double)1/98556321
or
1/(double)98556321
Which explicitely convert the number to double.
Happy coding....

Resources