Revision: 4349 http://vexi.svn.sourceforge.net/vexi/?rev=4349&view=rev Author: mkpg2 Date: 2012-02-02 04:56:46 +0000 (Thu, 02 Feb 2012) Log Message: ----------- Fix. Rounding when scale < 0.
Modified Paths: -------------- trunk/org.vexi-library.value/src/main/java/org/vexi/value/Rational.java trunk/org.vexi-library.value/src/test/java/org/vexi/value/TestRational.java Added Paths: ----------- trunk/org.vexi-library.value/src/poke/java/poke/PokeRational.java Modified: trunk/org.vexi-library.value/src/main/java/org/vexi/value/Rational.java =================================================================== --- trunk/org.vexi-library.value/src/main/java/org/vexi/value/Rational.java 2012-02-01 03:07:59 UTC (rev 4348) +++ trunk/org.vexi-library.value/src/main/java/org/vexi/value/Rational.java 2012-02-02 04:56:46 UTC (rev 4349) @@ -57,7 +57,16 @@ } static public Rational valueOf(BigDecimal n) { - return valueOf(n.unscaledValue(), BigInteger.TEN.pow(n.scale())); + BigInteger unscaled = n.unscaledValue(); + int scale = n.scale(); + if(scale==0){ + return valueOf(unscaled); + }else if(scale>0){ + return valueOf(unscaled, BigInteger.TEN.pow(scale)); + }else{ + return valueOf(unscaled.multiply(BigInteger.TEN.pow(-scale))); + } + } static final private BigInteger BI_TWO = BigInteger.valueOf(2); Added: trunk/org.vexi-library.value/src/poke/java/poke/PokeRational.java =================================================================== --- trunk/org.vexi-library.value/src/poke/java/poke/PokeRational.java (rev 0) +++ trunk/org.vexi-library.value/src/poke/java/poke/PokeRational.java 2012-02-02 04:56:46 UTC (rev 4349) @@ -0,0 +1,27 @@ +package poke; + +import junit.framework.TestCase; + +import org.vexi.value.Rational; + +public class PokeRational extends TestCase{ + + + static public void dump(String name, Rational r) { + System.out.println(name+" "+r); + System.out.println(); + } + + static public void main(String args[]) { + Rational r1 = Rational.valueOf(31400); + Rational r2 = Rational.valueOf(111, 7); + dump("r1", r1); + dump("r2", r2); + dump("r1 + r2", r1.add(r2)); + dump("r1 - r2", r1.subtract(r2)); + dump("r1 * r2", r1.multiply(r2)); + dump("r1 / r2", r1.divide(r2)); + dump("r2 ^ 2", r2.pow(2)); + } + +} Property changes on: trunk/org.vexi-library.value/src/poke/java/poke/PokeRational.java ___________________________________________________________________ Added: svn:mime-type + text/plain Modified: trunk/org.vexi-library.value/src/test/java/org/vexi/value/TestRational.java =================================================================== --- trunk/org.vexi-library.value/src/test/java/org/vexi/value/TestRational.java 2012-02-01 03:07:59 UTC (rev 4348) +++ trunk/org.vexi-library.value/src/test/java/org/vexi/value/TestRational.java 2012-02-02 04:56:46 UTC (rev 4349) @@ -1,21 +1,13 @@ package org.vexi.value; -public class TestRational { - static public void dump(String name, Rational r) { - System.out.println(name+" "+r); - System.out.println(); +import junit.framework.TestCase; + +public class TestRational extends TestCase{ + + public void testRound() throws ValueException { + assertEquals(Rational.valueOf(1230), Rational.valueOf(1234.5678).round(-1)); + assertEquals(Rational.tryParse("1234.57"), Rational.valueOf(1234.5678).round(2)); } - - static public void main(String args[]) { - Rational r1 = Rational.valueOf(31400); - Rational r2 = Rational.valueOf(111, 7); - dump("r1", r1); - dump("r2", r2); - dump("r1 + r2", r1.add(r2)); - dump("r1 - r2", r1.subtract(r2)); - dump("r1 * r2", r1.multiply(r2)); - dump("r1 / r2", r1.divide(r2)); - dump("r2 ^ 2", r2.pow(2)); - } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Keep Your Developer Skills Current with LearnDevNow! The most comprehensive online learning library for Microsoft developers is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3, Metro Style Apps, more. Free future releases when you subscribe now! http://p.sf.net/sfu/learndevnow-d2d _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn