Is there an elegant way of displaying fractions in C#

  • Thread starter Thread starter Bob
  • Start date Start date
B

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 ...
 
Bob said:
[...]
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
 
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 said:
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
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top