Use the following custom format:
This is basically a conditional format. Pseudocode:
If CellValue == 0
Display CellValue to up to 2 decimal places
I ran this through a few examples in Excel and got these. Does this meet your needs?
| 0 | 0|
| 12.1234 | 12.12|
| 12.1278 | 12.13|
| 12.1 | 12.1|
| -15.123 | -15.12|
| -24.9 | -24.9|
It looks like this doesn't solve the trailing decimal problem either. Sorry for getting your hopes up. My quick searches showed a couple people saying that this one isn't solvable through normal format codes. It needs to be VBA.
Apply Conditional Formatting for non-decimal numbers.
For example, A1 is the target cell.
- Format A1 as "###,###.00". This will be used for decimal number.
- Define Conditional Formatting for non-decimal numbers.
- Condition: Cell Value equal to =TRUNC(A1).
Below is the result:
12 => 12
14.231 => 14.23
15.00000 => 15
17.3 => 17.30
I ran into this recently in Excel 2007 and just ended up using the 'TRUNC' function in the value cells:
Worked exactly the way I wanted it to...
Excel custom formats can provide a partial answer
Custom formats for numbers in Excel are entered in this format:
- positive number format;negative number format;zero format;text format
One format that comes close to your requirement, but leaves in the decimal place for numbers with no decimals is:
- 15 is displayed as 15.
- 14.3453453 is displayed as 14.35
- 12.1 is displayed as 12.1
- 0 is displayed as 0
Here's one method using conditional formatting.
- Right click your cell, choose "Format Cell"
- Select "Custom"
- Enter "0.####" (add more or fewer #s to control the maximum number of decimal places)
- Click OK
- With the cell still selected, use "Conditional Formatting" (may be a menu item in Format or button in Home depending on your Excel version)
- Add a new rule, based on the formula "=MOD(H32,1)=0"
edit - better formula is "=MOD(ROUND(H32,2),1)=0" with the '2' being the desired number of decimal places. previous formula leave trailing decimal point if the number rounds to an integer.
Replace H32 in the formula with the ID of your cell, but MAKE SURE THERE ARE NO $ SIGNS! (No $ signs ensures that you to copy the format to other cells)
- For the format of this rule, select the number tab, choose "Custom"
- This time, enter the formula "0"
- Click OK enough times to return to the sheet (varies among excel versions)
There you go, enjoy responsibly! If you want to copy the format to other cells, remember that you can copy the cell and use "Paste Special" with the "Format" option.
Alternatively, you can solve the "optional" decimal point problem using the following solution:
This will add the decimal place and fractional value accordingly, while formatting negative numbers in a more legible way.
As for the poster's original question, you still need to round/truncate the "extra" decimal points using a formula. However, this is a simple formula of
=ROUND(<value>,<desired decimal places>) that is not extremely computationally expensive.
2500 -> 2500
0.25 -> 0.25
-2500 -> (2500)
-0.25 -> (0.25)
Remember that in Reporting Services, you can write an expression for the number formatting, too. In my situation, I used
as the number format expression. This produced an Excel file with two cell formats, selected by whether or not the value was an integer.
Are you / your users inputting values directly in the cells, or are they being populated by a formula or a macro?
If the cells are not being populated directly by a human, you could store the calculated values in a hidden range and then display formatted text to the user with a formula like this:
(where 'A1' is the cell being referenced)
The formula will display values like this:
| 15 | 15|
| 14.3453453 | 14.35|
| 12.1 | 12.1|
| 0 | 0|
| -15.123 | -15.12|
| 1.004 | 1|
NB: The formula output is a text string, not a numeric, so:
- The output defaults to being left-aligned.
- You cannot use the output in any further calculations (instead, you should use the original cell being referenced)
Format cell as 'General' then under data validation restrict values to decimals
This doesn't have to be quite so complicated - it can be done exclusively with Conditional Formatting. I have Excel 2010, so this may not work for earlier versions.
- Format the cells to be General. The number of decimal places for some numbers may be very high.
- Highlight a cell and click Conditional Formatting. Click New Rule, then "Use a Formula".
- Use the formula =(B1-INT(B1))>0 (where B1 is the cell you're formatting)
- Click the Format button and choose Custom. Enter [=0]0;.## into the Type field.
- Hit Enter several times.
- Click on Conditional Formatting again, click Manage Rules, and Edit the rule you just created. Change the range in the "Applies to" field to cover the range you want covered.
Further to Luke's great answer above here is a variation based on his work. I didn't want to have a zero in the cell if there was no value. Leave your cell format as general and used this formula:
Again where A1 is the cell being referenced.
Round function to provide significant figures. This does not change the number of decimal places displayed however. Just format the cell with
general number format though.
ROUND(MyValue,Sig.figs - 1 - INT(LOG10(ABS(MyValue)))
No more orphan decimal points.
This seems to be the simplest way to have a Decimal shown when there are fractional numbers - and No Decimal shown for whole numbers:
or with cell references:
15 = 15
9.23432234 = 9.23