473,386 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Is there an elegant way of displaying fractions in C#

Bob

Hi All,

Was wondering if in C# there is an elegant way of displaying and or calculating
fractions.

The case: we have an app that measures/slices dices etc and all our internal
measures and counters are in metric measures ... (its a manufacturing tool)
For display purposes this is fine for Australia/Europe/Asia .. however
the US company likes to see the measures in feet/inches .. no real problem
here.

However they'd like to see 1 3/4 inches rather than 1.75 inches .. and apparently
its not possible to make the whole country go metric by the time we need
to roll out.

I'm wondering if there is *anything* in the .net framework to make working
with and displaying fractions any easier ...

Cheers Sam ...
Aug 14 '07 #1
4 3353
Bob wrote:
[...]
However they'd like to see 1 3/4 inches rather than 1.75 inches .. and
apparently its not possible to make the whole country go metric by the
time we need to roll out.
I don't see why not. After all, we've been talking about it for at
least three decades. You'd think we'd be done by now. :)
I'm wondering if there is *anything* in the .net framework to make
working with and displaying fractions any easier ...
Sure. The System.Math class has methods that can help. Things like
Truncate, Round, etc. if you want, depending on how you might implement
the string formatting yourself.

Assuming the fractions are going to have a consistent denominator, or at
least some small number of consistent denominators, it should be simple
to implement it yourself. The exact implementation will depend on
things like whether you are to always use a specific denominator, if
you're supposed reduce fractions, and whether you're supposed to round
to the nearest fraction. But those shouldn't be too hard to deal with.

But there isn't any built-in string formatting that does fractions, if
that's what you're asking, no.

Pete
Aug 14 '07 #2
Bob wrote:
Was wondering if in C# there is an elegant way of displaying and or
calculating fractions.

The case: we have an app that measures/slices dices etc and all our
internal measures and counters are in metric measures ... (its a
manufacturing tool)
For display purposes this is fine for Australia/Europe/Asia .. however
the US company likes to see the measures in feet/inches .. no real
problem here.

However they'd like to see 1 3/4 inches rather than 1.75 inches .. and
apparently its not possible to make the whole country go metric by the
time we need to roll out.
I'm wondering if there is *anything* in the .net framework to make
working with and displaying fractions any easier ...
No. But it is not that difficult to code.

Here are something to get you started:

public static string FormatFraction(double x)
{
int ipart = (int)x;
double fpart = x - ipart;
for(int i = 2; i < 100; i++)
{
double scaledf = fpart * i;
if(Math.Abs(scaledf - (int)scaledf) < 0.0001)
{
if(ipart 0)
{
return ipart + " " + (int)scaledf + "/" + i;
}
else
{
return (int)scaledf + "/" + i;
}
}
}
throw new ArgumentException("Not a simple fraction: " + x);
}

Arne
Aug 14 '07 #3
It's funny, but our kids are being taught metrics, but seem to forget it
about age 15. They need to study for drivers licenses and all the traffic
signs are in US units....

I would make a lookup table for the fractional representation. You could
then round to the nearest value in the table. This is typically found in
manuals, and besides, if you had 1.33 inches, your user probably wants 1 3/8
not 1 33/100. With a lookup table you can make the normal (tape measure)
breakpoints.
"Bob" wrote:
>
Hi All,

Was wondering if in C# there is an elegant way of displaying and or calculating
fractions.

The case: we have an app that measures/slices dices etc and all our internal
measures and counters are in metric measures ... (its a manufacturing tool)
For display purposes this is fine for Australia/Europe/Asia .. however
the US company likes to see the measures in feet/inches .. no real problem
here.

However they'd like to see 1 3/4 inches rather than 1.75 inches .. and apparently
its not possible to make the whole country go metric by the time we need
to roll out.

I'm wondering if there is *anything* in the .net framework to make working
with and displaying fractions any easier ...

Cheers Sam ...
Aug 14 '07 #4
SAM
Hello Peter,
>
But there isn't any built-in string formatting that does fractions, if
that's what you're asking, no.
Yep thats what I was hoping for .. one of those magic .net framework moments
when you find that class that was designed as if by magic just for me ..
*sigh*

Guess I'll have to do some work then ..

thanks Guys.
Aug 15 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: smjmitchell | last post by:
Hi All, I need to display some equations on a form in VB (I will print the result in a text box beside the equation). The equations will in some cases be quite complicated and include...
33
by: selowitch | last post by:
I've been searching in vain for a way to present typographically correct fractions (not resorting to <sup> and <sub> tags) but have been frustrated by the fact that the glyphs for one-half,...
3
by: Chanchito | last post by:
hi there, I am seeking some guidance in regards to creating a query. I would like to be able to have the query display records that have had a certain amount of time pass since the time that is...
5
by: Steffen | last post by:
Hi, is it possible to have two fractions, which (mathematically) have the order a/b < c/d (a,b,c,d integers), but when (correctly) converted into floating point representation just have the...
19
by: Melvin G. Sheppers | last post by:
I want to display the time in a particular timezone (US Central) regardless where the computer is located. I have written the script below which displays the time in the Eastern, Central, Mountain,...
2
by: Bob | last post by:
Greetings All, Is there a simple way of suppressing the fractional part of a timespan At the moment I use one time span to populate another minus the fractional part .. this is a bit clunky. ...
1
by: Semajthewise | last post by:
Here it is cleaned up a little more. Here's what this code does. It will take 2 fractions and add, subtract, multiply, or divide them. The user enters the fractions to be calculated into two...
5
by: gubbachchi | last post by:
Hi all, How to convert the fractions into decimals and vice versa in php. I have a form, where the user will enter fractions in the text boxes such as "1 1/2", "1 1/4" and so on. I need to store...
0
by: Paddy | last post by:
(From: http://paddy3118.blogspot.com/2008/09/python-fractions-issue.html) There seems to be a problem/difference in calculating with the new fractions module when comparing Python 26rc2 and 30rc1...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.