By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,672 Members | 1,628 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,672 IT Pros & Developers. It's quick & easy.

Is there an elegant way of displaying fractions in C#

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.