If Statements Based on Number of Decimal Places?

Mydako

New Member
Joined
Apr 19, 2018
Messages
23
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,
 

Scott T

Well-known Member
Joined
Dec 14, 2016
Messages
2,556
Office Version
365, 2016
Platform
Windows
Maybe something like
<b></b><table cellpadding="2.5px" rules="all" style=";background-color: rgb(255,255,255);border: 1px solid;border-collapse: collapse; border-color: rgb(187,187,187)"><colgroup><col width="25px" style="background-color: rgb(218,231,245)" /><col /><col /></colgroup><thead><tr style=" background-color: rgb(218,231,245);text-align: center;color: rgb(22,17,32)"><th></th><th>A</th><th>B</th></tr></thead><tbody><tr ><td style="color: rgb(22,17,32);text-align: center;">2</td><td style="text-align: right;;">27.23</td><td style="text-align: right;;">108.92</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">3</td><td style="text-align: right;;">30.496</td><td style="text-align: right;;">152.48</td></tr></tbody></table><p style="width:4.8em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid rgb(187,187,187);border-top:none;text-align: center;background-color: rgb(218,231,245);color: rgb(22,17,32)">Sheet1</p><br /><br /><table width="85%" cellpadding="2.5px" rules="all" style=";border: 2px solid black;border-collapse:collapse;padding: 0.4em;background-color: rgb(255,255,255)" ><tr><td style="padding:6px" ><b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: rgb(255,255,255);border-collapse: collapse; border-color: rgb(187,187,187)"><thead><tr style=" background-color: rgb(218,231,245);color: rgb(22,17,32)"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: rgb(218,231,245);color: rgb(22,17,32)">B2</th><td style="text-align:left">=IF(<font color="Blue">LEN(<font color="Red">MID(<font color="Green">A2,SEARCH(<font color="Purple">".",A2</font>)+1,999</font>)</font>)<=2,A2*4,A2*5</font>)</td></tr><tr><th width="10px" style=" background-color: rgb(218,231,245);color: rgb(22,17,32)">B3</th><td style="text-align:left">=IF(<font color="Blue">LEN(<font color="Red">MID(<font color="Green">A3,SEARCH(<font color="Purple">".",A3</font>)+1,999</font>)</font>)<=2,A3*4,A3*5</font>)</td></tr></tbody></table></td></tr></table><br />
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
22,686
Something built on this might work for you
=IF((100*A1)=INT(100*A1),"2 or fewer D.P.", "other")
 

Mydako

New Member
Joined
Apr 19, 2018
Messages
23
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
 

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
8,787
Maybe:

ABCD
1AmountQtyAmt 1-4Amt 5
2152.48530.4930.52

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet5

Worksheet Formulas
CellFormula
C1="Amt 1-"&IF(C2*B2=A2,B2,B2-1)
D1=IF(C2*B2=A2,"","Amt "&B2)
C2=ROUNDDOWN(A2/B2,2)
D2=IF(D1="","",A2-(C2*(B2-1)))

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>

Put your values in A2 and B2, and the rest will calculate for you.
 

Scott T

Well-known Member
Joined
Dec 14, 2016
Messages
2,556
Office Version
365, 2016
Platform
Windows
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.
 

Mydako

New Member
Joined
Apr 19, 2018
Messages
23
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.
 

Forum statistics

Threads
1,077,795
Messages
5,336,374
Members
399,078
Latest member
johnk94

Some videos you may like

This Week's Hot Topics

Top