+ Reply to Thread
Results 1 to 4 of 4

Rounding with zeros

  1. #1
    Registered User
    Join Date
    09-21-2021
    Location
    Belarus, Minsk
    MS-Off Ver
    2019
    Posts
    7

    Rounding with zeros

    In cell D1 there is a number: 0.0015, need to round the values in cells A1; B1; C1 to the number of decimal places in cell D1, taking into account zeros. The number that will be in cell D1 may have a different number of decimal places. That is, need to round columns A, B, C to the number of characters, as in column D for each row. The code should work on any change in the sheet.

    The example indicates what is given and what the result should be. Thanks.
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    13,606

    Re: Rounding with zeros

    Are there other requirements? I see two possible approaches:

    1) Use the TEXT() function to convert the numbers to text in the desired number format. The LEN() function should be able to tell you how many digits are in column D (fn 1), which should allow you to build the number format code for the format portion of the TEXT() function. I generally don't like to convert numbers to text as it generally creates more troubles.

    2) Use conditional formatting (https://www.excel-easy.com/data-****...ormatting.html ). Again, use the LEN() function to determine how many digits are in column D (fn 1), then use that to determine which number format to apply. The main complication here is you need a conditional formatting rule for all possible scenarios:
    function rule -- format
    =LEN($D1)=3 -- "0.0"
    =LEN($D1)=4 -- "0.00"
    and so on
    It will be a bit tedious to set up, but, once set up, it should work fine.

    Which approach would you prefer to use?

    fn 1. One possible complication, where you are working with fractions/decimals/reals/doubles is floating point error. If the value in D1 is seen by Excel to be a couple bits different from the displayed value, the LEN() function will return 15 (the length of a double precision floating point number), and you won't be able to tell if it is truly 15 digits long or if it is floating point error.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Registered User
    Join Date
    09-21-2021
    Location
    Belarus, Minsk
    MS-Off Ver
    2019
    Posts
    7

    Re: Rounding with zeros

    Not good for me. can it be done with the help of a vba? like this?
    Please Login or Register  to view this content.
    but here I am binding to cells, how can I do that for columns?

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    13,606

    Re: Rounding with zeros

    VBA can do it. I don't see any way around "binding to a cell". The VBA procedure would need to loop through the rows.

    In the end, I don't see what the VBA would do differently from the conditional formatting approach I proposed, but, if you prefer VBA, then it can be done that way.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Minimum or Small Formula Help exclude zeros, nth term, multiple zeros
    By excelas88 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-21-2016, 02:43 PM
  2. [SOLVED] Removing Leading Zeros, maintaining zeros after decimal
    By Waycool86 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 03-17-2015, 01:15 PM
  3. [SOLVED] Average for non-consecutive cells excluding zeros (even when they all contain zeros)
    By pao13 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-05-2012, 04:32 PM
  4. [SOLVED] Sorting Filters for Zeros And delete The rows containing zeros
    By theprincesunil in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-25-2012, 06:10 AM
  5. Replies: 2
    Last Post: 06-12-2012, 02:24 PM
  6. Replies: 2
    Last Post: 02-22-2008, 11:18 AM
  7. Replies: 1
    Last Post: 05-04-2005, 02:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1