If Statements Based on Number of Decimal Places?

Mydako

New Member
Joined
Apr 19, 2018
Messages
26
Hello all,

I was wondering if it is possible to create an if statement that does different things depending on how many decimal places appear in a number?

For example if I have the numbers 27.23 and 30.496 can I do an if statement that multiples 27.23 by 4, but multiplies 30.496 by 5?

What I need is a way to do different things to numbers depending on if they have 2 (or less) decimal places, and 3 (or more) decimal places.

Is this possible?


Long Version Context:

For work I handle an Amazon account. Sometimes someone will order something with shipping, gift wrapping, the wrong tax, etc, and our system isn't equipped to handle that, so we have to wrap the costs into the item price. So Product X usually sells for $30, but one order had extra shipping for $2.48, so that 2.48 gets added into the item price. If it was one item, that's easy, 32.48. But if someone buys five for $150, and the shipping is the same 2.48, the item price becomes $152.48 total, but prices have to be entered individually, so divided by five it becomes 30.496 Obviously we can't enter something with three decimals, so what I have to do is enter four at one price, and the fifth at another. 4x30.49 (30.496 rounded down) and 1 at 30.52, so it equals the proper total.

I have to do this 50-100 times. So I'm looking for a way to automate the math with an if statement: Essentially if item price + extra charges / items sold has two (or less) decimal places, just return that number, but if the result has three (or more) decimal places it would do a different function. I can work out the function I needed, just don't know how to set it so it only applies to numbers with three or more decimals.

Thanks,
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Maybe something like

Book1
AB
227.23108.92
330.496152.48
Sheet1
Cell Formulas
RangeFormula
B2=IF(LEN(MID(A2,SEARCH(".",A2)+1,999))<=2,A2*4,A2*5)
B3=IF(LEN(MID(A3,SEARCH(".",A3)+1,999))<=2,A3*4,A3*5)
 
Upvote 0
Thank you both for you help.

Mike, that's a really good, simple way of figuring it out. Annoyed I didn't think of it.

Scott, are you able to explain a bit about how/why yours works? I get checking the length, starting in the middle, using the decimal point...but the +1999 is where I'm lost.

Thanks again
 
Upvote 0
Maybe:

ABCD
AmountQtyAmt 1-4Amt 5

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]
[TD="align: right"]152.48[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]30.49[/TD]
[TD="align: right"]30.52[/TD]

</tbody>
Sheet5

[TABLE="width: 85%"]
<tbody>[TR]
[TD]Worksheet Formulas[TABLE="width: 100%"]
<thead>[TR="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]
[TH="width: 10px"]Cell[/TH]
[TH="align: left"]Formula[/TH]
[/TR]
</thead><tbody>[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]C1[/TH]
[TD="align: left"]="Amt 1-"&IF(C2*B2=A2,B2,B2-1)[/TD]
[/TR]
[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]D1[/TH]
[TD="align: left"]=IF(C2*B2=A2,"","Amt "&B2)[/TD]
[/TR]
[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]C2[/TH]
[TD="align: left"]=ROUNDDOWN(A2/B2,2)[/TD]
[/TR]
[TR]
[TH="width: 10px, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=DAE7F5]#DAE7F5[/URL] "]D2[/TH]
[TD="align: left"]=IF(D1="","",A2-(C2*(B2-1)))[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]

Put your values in A2 and B2, and the rest will calculate for you.
 
Upvote 0
Thank you both for you help.

Mike, that's a really good, simple way of figuring it out. Annoyed I didn't think of it.

Scott, are you able to explain a bit about how/why yours works? I get checking the length, starting in the middle, using the decimal point...but the +1999 is where I'm lost.

Thanks again
To get only the digits after the decimal I used the mid function since I do not know how many digits there are I just used a large number.
 
Upvote 0
Thanks Eric! That automated more of that than I expected, thanks muchly.

Scott, okay, now I get it, I thought there was something specific about 1999 being used, rather than just generic large number. Thanks for explaining that.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top