Formatting decimals and whole numbers

thandel
thandel used Ask the Experts™
on
I have a text box on a form that displays some calculation based on the form.  All is working well but sometimes I have to display a decimal and other times a whole number.  This number represents month(s) so if the value is whole I don't want to display a decimal and when its not whole I want to display to one tenth of the value.

i.e. 12 months vs. 0.5 months.

I'm using Format (MyVariable, "0.0") which works OK but then the decimal is always displayed.

Any ideas?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You could try rounding the number instead :

Round( MyVariable, 0 )

This will always give whole numbers rather than the decimal places.

Or you can use :

Fix( MyVariable )

to fix the number and loose the decimal place completely, so 9.6 would give 9

Author

Commented:
I need to keep the decimal when its less than 1.0 (i.e. .5 or .7)  I think your solutions will always remove the decimal.

Author

Commented:
I'm thinking that I need to first determine if the number is whole or not.  Is there any better way then simply

If Right (Myvariable,1) <> 0 then.......
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2015
Distinguished Expert 2018
Commented:
How about:

Format (MyVariable, IIf(Int(MyVariable) = MyVariable, "0", "0.0"))

/gustav

Author

Commented:
Love it!
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Great!

/gustav

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial