Easy way to display non-zero decimal places?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
Office Version
  1. 365
Platform
  1. Windows
I have a UDF that calculates the odds for various events. One of the formats it can use for the result is the fractional odds (5:1, 3/4, etc.). I would like to display these in the most compact form possible. That is, no trailing zeroes. Here's some sample data the results I want:

[TABLE="class: grid, width: 250"]
<tbody>[TR]
[TD="align: center"]Actual Result[/TD]
[TD="align: center"]Desired Display[/TD]
[/TR]
[TR]
[TD="align: center"]5.000/1.000[/TD]
[TD="align: center"]5/1[/TD]
[/TR]
[TR]
[TD="align: center"]2.500/1.000[/TD]
[TD="align: center"]2.5/1[/TD]
[/TR]
[TR]
[TD="align: center"]1.250/1.000[/TD]
[TD="align: center"]1.25/1[/TD]
[/TR]
[TR]
[TD="align: center"]0.667/1.000[/TD]
[TD="align: center"]0.667/1[/TD]
[/TR]
[TR]
[TD="align: center"]1.000/2.000[/TD]
[TD="align: center"]1/2[/TD]
[/TR]
[TR]
[TD="align: center"]1.000/1.250[/TD]
[TD="align: center"]1/1.25[/TD]
[/TR]
[TR]
[TD="align: center"]1.000/1.333[/TD]
[TD="align: center"]1/1.333[/TD]
[/TR]
</tbody>[/TABLE]

My thought is to format it as "0.000" and then remove trailing zeroes. Is there a better way?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Maybe ...

[Table="width:, class:grid"][tr][td="bgcolor:#C0C0C0"][/td][td="bgcolor:#C0C0C0"]
A​
[/td][td="bgcolor:#C0C0C0"]
B​
[/td][td="bgcolor:#C0C0C0"]
C​
[/td][/tr][tr][td="bgcolor:#C0C0C0"]
2​
[/td][td]
0.5512​
[/td][td="bgcolor:#CCFFCC"]
1/1.81​
[/td][td="bgcolor:#CCFFCC"]B2: =IF(A2 < 1, TEXT(1/A2, "1\/0.00"), TEXT(A2, "0.00\/1"))[/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
3​
[/td][td]
1.4449​
[/td][td="bgcolor:#CCFFCC"]
1.44/1​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
4​
[/td][td]
1.0035​
[/td][td="bgcolor:#CCFFCC"]
1.00/1​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
5​
[/td][td]
0.5963​
[/td][td="bgcolor:#CCFFCC"]
1/1.68​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
6​
[/td][td]
0.8139​
[/td][td="bgcolor:#CCFFCC"]
1/1.23​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
7​
[/td][td]
0.85​
[/td][td="bgcolor:#CCFFCC"]
1/1.18​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
8​
[/td][td]
0.576​
[/td][td="bgcolor:#CCFFCC"]
1/1.74​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
9​
[/td][td]
0.4424​
[/td][td="bgcolor:#CCFFCC"]
1/2.26​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
10​
[/td][td]
2.9919​
[/td][td="bgcolor:#CCFFCC"]
2.99/1​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
11​
[/td][td]
0.4196​
[/td][td="bgcolor:#CCFFCC"]
1/2.38​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
12​
[/td][td]
0.5974​
[/td][td="bgcolor:#CCFFCC"]
1/1.67​
[/td][td][/td][/tr]
[tr][td="bgcolor:#C0C0C0"]
13​
[/td][td]
2.7854​
[/td][td="bgcolor:#CCFFCC"]
2.79/1​
[/td][td][/td][/tr]
[/table]
 
Upvote 0
Shg,

I probably didn't make myself clear enough. The UDF has two values A & B (A"/"B). I want to format both numbers separately to remove trailing zeroes. I think your method does the division first, no?

I just found this post from you from 2 years ago:

https://www.mrexcel.com/forum/excel...move-leading-trailing-zeros-item-numbers.html

This seems like a good solution, which I will apply to both values and then construct the ratio as a string.
 
Upvote 0
I have a UDF that calculates the odds for various events. One of the formats it can use for the result is the fractional odds (5:1, 3/4, etc.). I would like to display these in the most compact form possible. That is, no trailing zeroes. Here's some sample data the results I want:

[TABLE="class: grid, width: 250"]
<tbody>[TR]
[TD="align: center"]Actual Result[/TD]
[TD="align: center"]Desired Display[/TD]
[/TR]
[TR]
[TD="align: center"]5.000/1.000[/TD]
[TD="align: center"]5/1[/TD]
[/TR]
[TR]
[TD="align: center"]2.500/1.000[/TD]
[TD="align: center"]2.5/1[/TD]
[/TR]
[TR]
[TD="align: center"]1.250/1.000[/TD]
[TD="align: center"]1.25/1[/TD]
[/TR]
[TR]
[TD="align: center"]0.667/1.000[/TD]
[TD="align: center"]0.667/1[/TD]
[/TR]
[TR]
[TD="align: center"]1.000/2.000[/TD]
[TD="align: center"]1/2[/TD]
[/TR]
[TR]
[TD="align: center"]1.000/1.250[/TD]
[TD="align: center"]1/1.25[/TD]
[/TR]
[TR]
[TD="align: center"]1.000/1.333[/TD]
[TD="align: center"]1/1.333[/TD]
[/TR]
</tbody>[/TABLE]

My thought is to format it as "0.000" and then remove trailing zeroes. Is there a better way?
You did not post your UDF code, so it is not possible to tell how your odds are calculated, but if you calculate the numerator and denominator separately to the lowest form, then I would think this might work...

Odds = Format(Numerator) & "/" & Format(Denominator)
 
Upvote 0
If you're happy, I'm happy.

Heh, well, you are easy to please. :laugh:

I was impressed by your clever solution in the other thread.
Code:
Function CCTrim(sInp As String) As String
   CCTrim = Replace(Trim(Replace(sInp, "0", " ")), " ", "0")
End Function

It took me a few minutes to figure out what you were doing. First you replace all of the zeroes with spaces, which includes embedded zeroes, then you remove (trim) leading and trailing spaces, then you replace the interior spaces with zeroes. It's very clever. I would not have thought of it in a million years. I love clever solutions like this. Thank you. :beerchug:
 
Upvote 0
You did not post your UDF code, so it is not possible to tell how your odds are calculated, but if you calculate the numerator and denominator separately to the lowest form, then I would think this might work...

Odds = Format(Numerator) & "/" & Format(Denominator)

I see that I neglected to mention that I want no more than 3 decimal places. Otherwise, this would work great. Thanks.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top