how to print out fractions

M

Mori

Hi,

Can someone supply a code example of displaying a string with a fractional
part, say 5 and 7 16ths.

I cannot find an example of how to use the Encoding object (if that is what
you use).

thanks,
Mori
 
N

Nicholas Paldino [.NET/C# MVP]

Mori,

I generally would stay away from this kind of thing. The reason is that
no matter what, you will never be able to have a character set that can
include all of the fractions that are possible. Not only are there space
considerations, but it is mathematically impossible (the range of numbers
for the top and bottom numbers are both infinite, and there is nothing on a
machine that can represent that).

This is why you are better of using some other representation.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mori said:
Nicholas,

Yes, as a single character.

There are a few fractions in the Microsoft Sans Serif font, so I have been
able to display those fine in Labels etc.

I was thinking of creating my own font for the program but I will also be
outputting to a Word document, so whoever reads the document will have to
have that font installed on their machine.

I just thought have one a character fraction is better looking than 7/16.

Thanks for your time,
Mori


in message news:%[email protected]...
Mori,

Do you mean as a single character? I don't think that you can do this
for any fraction you wish (as far as I know), because character sets have
certain fractions pre-defined. If it is a rich text box, then you might be
able to superscript and subscript some text, and that will give you the
result you want, but I don't know if an Encoding object will help in this
case.

What are you using to display the fraction?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mori said:
Hi,

Can someone supply a code example of displaying a string with a fractional
part, say 5 and 7 16ths.

I cannot find an example of how to use the Encoding object (if that is what
you use).

thanks,
Mori
 
M

Mark Mullin

C# doesn't explicitly (afaik) have a means for managing rational
numbers as distinct object. Encoder supports the use of fractions,
but that's just for accurate specification, not because it really has
that much of a clue.

If you're looking for intelligent output, i.e. if you've given the
fraction
5 4/16ths, and want to get back 5 1/4th, you need to implement a
fraction class that can normalize the fraction, i.e. reduce it. The
following link is to C++ code, but the Normalization function provided
should give you an idea of whats needed.

MMM

http://www.codeproject.com/useritems/cfraction.asp
 

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

Top