Math - Fractions 3/2 to 1 1/2

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Math - Fractions 3/2 to 1 1/2

paulbeuk
Hello,

Since yesterday I'm using commons-math-2.1.jar to calculate with fractions.
It serves my well. There's one last feature I'd like to use, and can not find how.

- how do you convert/format fraction 3/2 to 1 1/2
- how do you convert/format fraction 4/2 to 2

Thanks in advance
Paul
Reply | Threaded
Open this post in threaded view
|

Re: Math - Fractions 3/2 to 1 1/2

sebb-2-2
On 23 November 2010 13:15, paulbeuk <[hidden email]> wrote:

>
> Hello,
>
> Since yesterday I'm using commons-math-2.1.jar to calculate with fractions.
> It serves my well. There's one last feature I'd like to use, and can not
> find how.
>
> - how do you convert/format fraction 3/2 to 1 1/2
> - how do you convert/format fraction 4/2 to 2
>

See http://commons.apache.org/math/userguide/fraction.html

> Thanks in advance
> Paul
>
> --
> View this message in context: http://apache-commons.680414.n4.nabble.com/Math-Fractions-3-2-to-1-1-2-tp3055391p3055391.html
> Sent from the Commons - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Math - Fractions 3/2 to 1 1/2

paulbeuk
Hello sebb-2-2

> See http://commons.apache.org/math/userguide/fraction.html

I have red that page, but can not find the answer to my question there. Can you be more specific?

FractionFormat format = new FractionFormat();
Fraction f = new Fraction(4, 2);
String s = format.format(f);
I want the outcome is human readable as possible, so 2 and not "4 / 2"


Fraction f = new Fraction(3, 2);
should give me 1 1/2 and not "3 / 2"

tia
Reply | Threaded
Open this post in threaded view
|

Re: Math - Fractions 3/2 to 1 1/2

Mikkel Meyer Andersen-2
Try to look at FractionFormat.getProperInstance(). Example:
            System.out.println(FractionFormat.getProperInstance().format(new
Fraction(5, 2)));
            System.out.println(FractionFormat.getImproperInstance().format(new
Fraction(5, 2)));
            System.out.println(FractionFormat.getProperInstance().format(new
Fraction(2, 5)));
            System.out.println(FractionFormat.getImproperInstance().format(new
Fraction(2, 5)));

2010/11/23 paulbeuk <[hidden email]>:

>
> Hello sebb-2-2
>
>> See http://commons.apache.org/math/userguide/fraction.html
>
> I have red that page, but can not find the answer to my question there. Can
> you be more specific?
>
> FractionFormat format = new FractionFormat();
> Fraction f = new Fraction(4, 2);
> String s = format.format(f);
> I want the outcome is human readable as possible, so 2 and not "4 / 2"
>
>
> Fraction f = new Fraction(3, 2);
> should give me 1 1/2 and not "3 / 2"
>
> tia
> --
> View this message in context: http://apache-commons.680414.n4.nabble.com/Math-Fractions-3-2-to-1-1-2-tp3055391p3055433.html
> Sent from the Commons - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Math - Fractions 3/2 to 1 1/2

paulbeuk
Hello Mikkel,

Thanks! (great for recipes)

FractionFormat format = FractionFormat.getProperInstance();
Fraction f = new Fraction(5,2);
String s = format.format(f).replaceAll(" / ", "/");
s = s.replaceAll(" 0/1", "");
System.out.println("s : " + s ); = 2 1/2

Fraction f = new Fraction(2,2);
String s = format.format(f).replaceAll(" / ", "/");
s = s.replaceAll(" 0/1", "");
System.out.println("s : " + s ); = 1