Results 1 to 8 of 8

Thread: If Statements Based on Number of Decimal Places?
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Apr 2018
    Posts
    23
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default If Statements Based on Number of Decimal Places?

    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,

  2. #2
    Board Regular Scott T's Avatar
    Join Date
    Dec 2016
    Posts
    2,525
    Post Thanks / Like
    Mentioned
    18 Post(s)
    Tagged
    1 Thread(s)

    Default Re: If Statements Based on Number of Decimal Places?

    Maybe something like
    AB
    227.23108.92
    330.496152.48

    Sheet1



    Worksheet Formulas
    CellFormula
    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)

    Colonel Sandurz: Prepare ship for light speed.
    Dark Helmet: No, no, no, light speed is too slow.
    Colonel Sandurz: Light speed, too slow?
    Dark Helmet: Yes, we're gonna have to go right to ludicrous speed.

  3. #3
    MrExcel MVP mikerickson's Avatar
    Join Date
    Jan 2007
    Location
    Davis CA
    Posts
    22,604
    Post Thanks / Like
    Mentioned
    20 Post(s)
    Tagged
    15 Thread(s)

    Default Re: If Statements Based on Number of Decimal Places?

    Something built on this might work for you
    =IF((100*A1)=INT(100*A1),"2 or fewer D.P.", "other")

  4. #4
    New Member
    Join Date
    Apr 2018
    Posts
    23
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: If Statements Based on Number of Decimal Places?

    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

  5. #5
    MrExcel MVP Eric W's Avatar
    Join Date
    Aug 2015
    Location
    Bountiful, UT
    Posts
    8,661
    Post Thanks / Like
    Mentioned
    42 Post(s)
    Tagged
    5 Thread(s)

    Default Re: If Statements Based on Number of Decimal Places?

    Maybe:

    A B C D
    1 Amount Qty Amt 1-4 Amt 5
    2 152.48 5 30.49 30.52
    Sheet5

    Worksheet Formulas
    Cell Formula
    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)))

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

    When you eliminate the impossible, whatever remains, however improbable, must be the truth.

    -Posting guidelines, forum rules, terms of use, FAQs, BB codes, See how to search the forum
    -Post a screen shot with the HTML Maker

  6. #6
    Board Regular Scott T's Avatar
    Join Date
    Dec 2016
    Posts
    2,525
    Post Thanks / Like
    Mentioned
    18 Post(s)
    Tagged
    1 Thread(s)

    Default Re: If Statements Based on Number of Decimal Places?

    Quote Originally Posted by Mydako View Post
    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.
    Colonel Sandurz: Prepare ship for light speed.
    Dark Helmet: No, no, no, light speed is too slow.
    Colonel Sandurz: Light speed, too slow?
    Dark Helmet: Yes, we're gonna have to go right to ludicrous speed.

  7. #7
    New Member
    Join Date
    Apr 2018
    Posts
    23
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: If Statements Based on Number of Decimal Places?

    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.

  8. #8
    MrExcel MVP Eric W's Avatar
    Join Date
    Aug 2015
    Location
    Bountiful, UT
    Posts
    8,661
    Post Thanks / Like
    Mentioned
    42 Post(s)
    Tagged
    5 Thread(s)

    Default Re: If Statements Based on Number of Decimal Places?

    Glad we could help!
    Cheers,
    Eric

    When you eliminate the impossible, whatever remains, however improbable, must be the truth.

    -Posting guidelines, forum rules, terms of use, FAQs, BB codes, See how to search the forum
    -Post a screen shot with the HTML Maker

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •