Repository: ionspin/kotlin-multiplatform-bignum (kotlin)

Issue:
Move point by pow of 10 - BigDecimal.movePointRight
**Is your feature request related to a problem? Please describe.**
No, just a feature request

**Describe the solution you'd like**
Implement (something similiar to) BigDecimal.movePointRight
While doing that, probably add movePointLeft too.
https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#movePointRight-int-

**Describe alternatives you've considered**
Scaling by power of 10 while maintaining positive position.

**Additional context**
https://stackoverflow.com/questions/25454734/what-is-the-difference-between-bigdecimal-movepointright-and-scalebypoweroften

Pull request:
Also fix extension function for int, long, short, byte conversion. Fixes #50

Patch:
diff --git a/README.md b/README.md
index 78fcba3b..8e6b82a0 100644
--- a/README.md
+++ b/README.md
@@ -306,7 +306,14 @@ Or for `Double` of `Float`
 ```kotlin
 val bigDecimalFromFloat = 123.456f.toBigDecimal() 
 val bigDecimalFromDouble = 123.456.toBigDecimal()
+```
 
+`Long`, `Int`, `Short`, `Byte` 
+```kotlin
+val bigDecimalFromLong = 10.toLong().toBigDecimal() 
+val bigDecimalFromInt = 10.toInt().toBigDecimal()
+val bigDecimalFromShort = 10.toShort().toBigDecimal() 
+val bigDecimalFromByte = 10.toByte().toBigDecimal()
 ```
 
 ## toString
@@ -347,7 +354,7 @@ Standard arithmetic operations that are present:
 * Signum
 
 
-(Suspiciously missing is square root, both from BigInteger and BigDecimal, should be added soon™)
+(Suspiciously missing is square root, should be added soon™)
 
 Operations are executed with existing significands and then rounded down afterwards. Decimal mode parameter controls the precision and rounding mode
 
diff --git a/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimal.kt b/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimal.kt
index cc441fab..6a903f5b 100644
--- a/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimal.kt
+++ b/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimal.kt
@@ -991,6 +991,9 @@ class BigDecimal private constructor(
         return significand.isZero()
     }
 
+    /**
+     * Creates a copy of this BigDecimal with some different elements (significand/exponent/decimalMode)
+     */
     fun copy(
         significand: BigInteger = this.significand,
         exponent: BigInteger = this.exponent,
@@ -998,6 +1001,15 @@ class BigDecimal private constructor(
     ): BigDecimal {
         return BigDecimal(significand, exponent, decimalMode)
     }
+
+    /**
+     * Moves the decimal point by creating a new instance with a different exponent. Positive values move
+     * the decimal point to the right, negative values move the decimal point to the left.
+     */
+    fun moveDecimalPoint(places: Int): BigDecimal {
+        return copy(exponent = exponent + places)
+    }
+
     // TODO in 0.3.0
 //    override fun pow(exponent: BigDecimal): BigDecimal {
 //        if (exponent.signum() < 0) {
@@ -1012,10 +1024,13 @@ class BigDecimal private constructor(
 //        return result
 //
 //    }
-
+    /**
+     * Exponentiate this BigDecimal by some exponent
+     */
     override fun pow(exponent: Int): BigDecimal {
         return pow(exponent.toLong())
     }
+
     // TODO in 0.3.0
 //    /**
 //     * Natural logarithm of this BigDecimal
@@ -1235,13 +1250,13 @@ class BigDecimal private constructor(
         return BigDecimal(bigDecimal.significand, newExponent)
     }
 
+    /**
+     * Brings the two big decimals to same exponents, which makes it easier to apply other operations
+     */
     private fun bringSignificandToSameExponent(
         first: BigDecimal,
         second: BigDecimal
     ): Triple<BigInteger, BigInteger, BigInteger> {
-//        if (first.exponent == second.exponent) {
-//            return Triple(first.significand, second.significand, first.exponent)
-//        }
         val firstPrepared = getRidOfRadix(first)
         val secondPrepared = getRidOfRadix(second)
 
diff --git a/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalExtensions.kt b/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalExtensions.kt
index 8f216c82..8ba9f4ce 100644
--- a/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalExtensions.kt
+++ b/bignum/src/commonMain/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalExtensions.kt
@@ -30,7 +30,7 @@ fun Long.toBigDecimal(exponent: BigInteger? = null, decimalMode: DecimalMode? =
     return if (exponent != null) {
         BigDecimal.fromLongWithExponent(this, exponent, decimalMode)
     } else {
-        BigDecimal.fromLongAsSignificand(this, decimalMode)
+        BigDecimal.fromLong(this, decimalMode)
     }
 }
 
@@ -39,7 +39,7 @@ fun Int.toBigDecimal(exponent: BigInteger? = null, decimalMode: DecimalMode? = n
     return if (exponent != null) {
         BigDecimal.fromIntWithExponent(this, exponent, decimalMode)
     } else {
-        BigDecimal.fromIntAsSignificand(this, decimalMode)
+        BigDecimal.fromInt(this, decimalMode)
     }
 }
 
@@ -48,7 +48,7 @@ fun Short.toBigDecimal(exponent: BigInteger? = null, decimalMode: DecimalMode? =
     return if (exponent != null) {
         BigDecimal.fromShortWithExponent(this, exponent, decimalMode)
     } else {
-        BigDecimal.fromShortAsSignificand(this, decimalMode)
+        BigDecimal.fromShort(this, decimalMode)
     }
 }
 
@@ -57,7 +57,7 @@ fun Byte.toBigDecimal(exponent: BigInteger? = null, decimalMode: DecimalMode? =
     return if (exponent != null) {
         BigDecimal.fromByteWithExponent(this, exponent, decimalMode)
     } else {
-        BigDecimal.fromByteAsSignificand(this, decimalMode)
+        BigDecimal.fromByte(this, decimalMode)
     }
 }
 


Test patch:
diff --git a/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalCreationTest.kt b/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalCreationTest.kt
index 9cd7cc11..0a821d53 100644
--- a/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalCreationTest.kt
+++ b/bignum/src/commonTest/kotlin/com/ionspin/kotlin/bignum/decimal/BigDecimalCreationTest.kt
@@ -123,4 +123,43 @@ class BigDecimalCreationTest {
             a == b
         }
     }
+
+    @Test
+    fun testMovePoint() {
+
+        assertTrue {
+            val original = 123.45.toBigDecimal()
+            val moved = original.moveDecimalPoint(0)
+            val expected = 123.45.toBigDecimal()
+            moved == expected
+        }
+
+        assertTrue {
+            val original = 123.45.toBigDecimal()
+            val moved = original.moveDecimalPoint(1)
+
+            moved == 1234.5.toBigDecimal()
+        }
+
+        assertTrue {
+            val original = 123.45.toBigDecimal()
+            val moved = original.moveDecimalPoint(3)
+            val expected = 123450.toBigDecimal()
+            moved == expected
+        }
+
+        assertTrue {
+            val original = 123.45.toBigDecimal()
+            val moved = original.moveDecimalPoint(-3)
+            val expected = 0.12345.toBigDecimal()
+            moved == expected
+        }
+
+        assertTrue {
+            val original = 123.45.toBigDecimal()
+            val moved = original.moveDecimalPoint(-5)
+            val expected = 0.0012345.toBigDecimal()
+            moved == expected
+        }
+    }
 }
\ No newline at end of file