Easy way to display non-zero decimal places?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,525
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:

Actual ResultDesired Display
5.000/1.0005/1
2.500/1.0002.5/1
1.250/1.0001.25/1
0.667/1.0000.667/1
1.000/2.0001/2
1.000/1.2501/1.25
1.000/1.3331/1.333

<tbody>
</tbody>

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

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Maybe ...

A​
B​
C​
2​
0.5512​
1/1.81​
B2: =IF(A2 < 1, TEXT(1/A2, "1\/0.00"), TEXT(A2, "0.00\/1"))
3​
1.4449​
1.44/1​
4​
1.0035​
1.00/1​
5​
0.5963​
1/1.68​
6​
0.8139​
1/1.23​
7​
0.85​
1/1.18​
8​
0.576​
1/1.74​
9​
0.4424​
1/2.26​
10​
2.9919​
2.99/1​
11​
0.4196​
1/2.38​
12​
0.5974​
1/1.67​
13​
2.7854​
2.79/1​
 
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
If you're happy, I'm happy.
 
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:

Actual ResultDesired Display
5.000/1.0005/1
2.500/1.0002.5/1
1.250/1.0001.25/1
0.667/1.0000.667/1
1.000/2.0001/2
1.000/1.2501/1.25
1.000/1.3331/1.333

<tbody>
</tbody>

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. :LOL:

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. (y)
 
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,214,649
Messages
6,120,730
Members
448,987
Latest member
marion_davis

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