G
Gordon Bentley-Mix
Ages ago I asked a question about formatting percentages (specifically in
TextBox controls on a UserForm) to display a plus or minus sign as
appropriate - something that the 'FormatPercent' function doesn't support. I
received a most helpful answer from either Jay or Doug - I've slept a few
times since then and can't remember exactly who it was - describing a method
for using the basic 'Format' function to meet my needs, and things have been
working a treat ever since.
Until now...
Here's the situation. I need a method whereby percentage values are
displayed with up to two digits after the decimal point but *only* if there
are digits to be displayed; for example: "50%", "33.3%", "6.25%".
Obviously FormatPercent isn't going to work for this because it only
supports specifying the number of digits after the decimal point, which
means a fixed number of digits (padded with trailing zeros) are always
displayed regardless; for example: either "50.00%", "33.30%", "6.25%" or
"50.0%", "33.3%", "6.2%" or "50%", "33%", "6%". Clearly less than ideal...
OTOH, using something like
Format(TextBox1.Value), "0.##%")
doesn't quite cut it either. The hash marks (#) do work as expected with
values that have one or two digits after the decimal point like "33.3%" and
"6.25%", but unfortunately I get things like "50.%" (note the extraneous
decimal point) when there are *no* digits after the decimal point.
Other than a "brute force" approach of doing something like parsing the
value using the Mid function to look for a decimal point in the penultimate
position and removing it, does anyone have a way around this? Or should I
get out my big hammer and change my username to Thor?
TextBox controls on a UserForm) to display a plus or minus sign as
appropriate - something that the 'FormatPercent' function doesn't support. I
received a most helpful answer from either Jay or Doug - I've slept a few
times since then and can't remember exactly who it was - describing a method
for using the basic 'Format' function to meet my needs, and things have been
working a treat ever since.
Until now...
Here's the situation. I need a method whereby percentage values are
displayed with up to two digits after the decimal point but *only* if there
are digits to be displayed; for example: "50%", "33.3%", "6.25%".
Obviously FormatPercent isn't going to work for this because it only
supports specifying the number of digits after the decimal point, which
means a fixed number of digits (padded with trailing zeros) are always
displayed regardless; for example: either "50.00%", "33.30%", "6.25%" or
"50.0%", "33.3%", "6.2%" or "50%", "33%", "6%". Clearly less than ideal...
OTOH, using something like
Format(TextBox1.Value), "0.##%")
doesn't quite cut it either. The hash marks (#) do work as expected with
values that have one or two digits after the decimal point like "33.3%" and
"6.25%", but unfortunately I get things like "50.%" (note the extraneous
decimal point) when there are *no* digits after the decimal point.
Other than a "brute force" approach of doing something like parsing the
value using the Mid function to look for a decimal point in the penultimate
position and removing it, does anyone have a way around this? Or should I
get out my big hammer and change my username to Thor?