# Rounding with zeros

1. ## 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.  Register To Reply

2. ## 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.  Register To Reply

3. ## 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?  Register To Reply

4. ## 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.  Register To Reply