问题
I'm trying to do the following in Google Sheets:
If the number in cell A1 has 4 decimals (for example "1.0001"), and if cell A1>B1 (for example A1=1.0001 and B1=0.0001), and if the string in cell C1 = "Good", then return (A1-B1)*10000.
Additionally:
If the number in cell A2 has 2 decimals (for example "1.01"), and if cell A2>B2 (for example A2=1.01 and B2=0.01), and if the string in cell C2 = "Great", then return (A2-B2)*100.
So far, I've come up with this IFS function:
=IFS((AND(A1>B1, C1="Good")), (A1-B1)*10000,(AND(A2>B2, C2="Great")),(A2-B2)*100,TRUE,"ERROR")
Which treats the two arguments A1>B1, C1="Good" / A2>B2, C2="Great", within the AND formula.
How can I add the decimal argument to the AND statement?
I thought of setting for something like :
=IFS((AND(A1>B1, C1="Good", **A1=(a number with 4 decimals)))**, (A1-B1)*10000,(AND(A2>B2, C2="Great", **A2=(a number with 2 decimals)))**,(A2-B2)*100,TRUE,"ERROR")
Where the statements:
A1=(a number with 4 decimals)
and
A1=(a number with 2 decimals)
would do the trick.
How do you formulate those missing "decimal statements"?
回答1:
It depends if you mean exactly 4 decimal places, like 1.0001, or at least 4 decimals, which would include 1.000123.
You can test for decimals by using round (or roundup or rounddown) so in the first case:
=ifs(and(round(A1,3)<>A1,A1>B1,C1="Good"),(A1-B1)*10000,and(round(A2,1)<>A2,A2>B2,C2="Great"),(A2-B2)*100)
But if you wanted it to be exactly 4 decimals:
=ifs(and(round(A1,3)<>A1,round(A1,4)=A1,A1>B1,C1="Good"),(A1-B1)*10000,and(round(A2,1)<>A2,round(A2,2)=A2,A2>B2,C2="Great"),(A2-B2)*100)
回答2:
you can solve this with one blow:
=ARRAYFORMULA(
IF((LEN(IFERROR(REGEXEXTRACT(TO_TEXT(A1:A), "\.(.*)")))=4) *
(A1:A>B1:B) * (C1:C="Good"), (A1:A-B1:B)*10000,
IF((LEN(IFERROR(REGEXEXTRACT(TO_TEXT(A1:A), "\.(.*)")))=2) *
(A1:A>B1:B) * (C1:C="Great"), (A1:A-B1:B)*100, )))
来源:https://stackoverflow.com/questions/56232416/how-to-make-a-condition-based-on-the-number-of-decimals-a-number-has-with-the-if