You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by paulbeuk <pa...@gmail.com> on 2010/11/23 14:15:09 UTC
Math - Fractions 3/2 to 1 1/2
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
--
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: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: Math - Fractions 3/2 to 1 1/2
Posted by paulbeuk <pa...@gmail.com>.
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
--
View this message in context: http://apache-commons.680414.n4.nabble.com/Math-Fractions-3-2-to-1-1-2-tp3055391p3055571.html
Sent from the Commons - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: Math - Fractions 3/2 to 1 1/2
Posted by Mikkel Meyer Andersen <mi...@mikl.dk>.
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 <pa...@gmail.com>:
>
> 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: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: Math - Fractions 3/2 to 1 1/2
Posted by paulbeuk <pa...@gmail.com>.
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: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: Math - Fractions 3/2 to 1 1/2
Posted by sebb <se...@gmail.com>.
On 23 November 2010 13:15, paulbeuk <pa...@gmail.com> 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: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org