Add missing api: radix overloads, mathContext and scale overloads, orNull overloads

Move string-to-number conversions and their tests near to existing ones

Add documentation and copyrights.
This commit is contained in:
Ilya Gorbunov
2017-05-24 08:33:15 +03:00
parent e86e080088
commit b89e5b2708
5 changed files with 309 additions and 39 deletions
@@ -340,6 +340,105 @@ public fun String.toFloatOrNull(): Float? = screenFloatValue(this, java.lang.Flo
@kotlin.jvm.JvmVersion
public fun String.toDoubleOrNull(): Double? = screenFloatValue(this, java.lang.Double::parseDouble)
/**
* Parses the string as a [java.math.BigInteger] number and returns the result.
* @throws NumberFormatException if the string is not a valid representation of a number.
*/
@SinceKotlin("1.2")
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun String.toBigInteger(): java.math.BigInteger =
java.math.BigInteger(this)
/**
* Parses the string as a [java.math.BigInteger] number and returns the result.
* @throws NumberFormatException if the string is not a valid representation of a number.
* @throws IllegalArgumentException when [radix] is not a valid radix for string to number conversion.
*/
@SinceKotlin("1.2")
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun String.toBigInteger(radix: Int): java.math.BigInteger =
java.math.BigInteger(this, checkRadix(radix))
/**
* Parses the string as a [java.math.BigInteger] number and returns the result
* or `null` if the string is not a valid representation of a number.
*/
@SinceKotlin("1.2")
@kotlin.jvm.JvmVersion
public fun String.toBigIntegerOrNull(): java.math.BigInteger? = toBigIntegerOrNull(10)
/**
* Parses the string as a [java.math.BigInteger] number and returns the result
* or `null` if the string is not a valid representation of a number.
*
* @throws IllegalArgumentException when [radix] is not a valid radix for string to number conversion.
*/
@SinceKotlin("1.2")
@kotlin.jvm.JvmVersion
public fun String.toBigIntegerOrNull(radix: Int): java.math.BigInteger? {
checkRadix(radix)
val length = this.length
when (length) {
0 -> return null
1 -> if (digitOf(this[0], radix) < 0) return null
else -> {
val start = if (this[0] == '-') 1 else 0
for (index in start until length) {
if (digitOf(this[index], radix) < 0)
return null
}
}
}
return toBigInteger(radix)
}
/**
* Parses the string as a [java.math.BigDecimal] number and returns the result.
* @throws NumberFormatException if the string is not a valid representation of a number.
*/
@SinceKotlin("1.2")
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun String.toBigDecimal(): java.math.BigDecimal =
java.math.BigDecimal(this)
/**
* Parses the string as a [java.math.BigDecimal] number and returns the result.
* @throws NumberFormatException if the string is not a valid representation of a number.
*
* @param mathContext specifies the precision and the rounding mode.
* @throws ArithmeticException if the rounding is needed, but the rounding mode is [java.math.RoundingMode.UNNECESSARY].
*/
@SinceKotlin("1.2")
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
public inline fun String.toBigDecimal(mathContext: java.math.MathContext): java.math.BigDecimal =
java.math.BigDecimal(this, mathContext)
/**
* Parses the string as a [java.math.BigDecimal] number and returns the result
* or `null` if the string is not a valid representation of a number.
*/
@SinceKotlin("1.2")
@kotlin.jvm.JvmVersion
public fun String.toBigDecimalOrNull(): java.math.BigDecimal? =
screenFloatValue(this) { it.toBigDecimal() }
/**
* Parses the string as a [java.math.BigDecimal] number and returns the result
* or `null` if the string is not a valid representation of a number.
*
* @param mathContext specifies the precision and the rounding mode.
* @throws ArithmeticException if the rounding is needed, but the rounding mode is [java.math.RoundingMode.UNNECESSARY].
*/
@SinceKotlin("1.2")
@kotlin.jvm.JvmVersion
public fun String.toBigDecimalOrNull(mathContext: java.math.MathContext): java.math.BigDecimal? =
screenFloatValue(this) { it.toBigDecimal(mathContext) }
/**
* Recommended floating point number validation RegEx from the javadoc of `java.lang.Double.valueOf(String)`
*/
+72 -12
View File
@@ -1,3 +1,19 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("MathKt")
@file:kotlin.jvm.JvmVersion
@@ -5,6 +21,7 @@
package kotlin
import java.math.BigDecimal
import java.math.MathContext
import java.math.RoundingMode
/**
@@ -67,31 +84,74 @@ public inline operator fun BigDecimal.inc(): BigDecimal = this.add(BigDecimal.ON
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.dec(): BigDecimal = this.subtract(BigDecimal.ONE)
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun String.toBigDecimal(): BigDecimal = BigDecimal(this)
/**
* Returns the value of this [Int] number as a [BigDecimal].
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Int.toBigDecimal(): BigDecimal = BigDecimal(this)
/**
* Returns the value of this [Int] number as a [BigDecimal].
* @param mathContext specifies the precision and the rounding mode.
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Int.toBigDecimal(mathContext: MathContext): BigDecimal = BigDecimal(this, mathContext)
/**
* Returns the value of this [Long] number as a [BigDecimal].
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Long.toBigDecimal(): BigDecimal = BigDecimal(this)
/*
* JDK documentation recommends using `BigDecimal(String)` instead of `BigDecimal(double)`
* For more details consult http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
/**
* Returns the value of this [Long] number as a [BigDecimal].
* @param mathContext specifies the precision and the rounding mode.
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Long.toBigDecimal(mathContext: MathContext): BigDecimal = BigDecimal(this, mathContext)
/**
* Returns the value of this [Float] number as a [BigDecimal].
*
* The number is converted to a string and then the string is converted to a [BigDecimal].
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Float.toBigDecimal(): BigDecimal = BigDecimal(this.toString())
/*
* JDK documentation recommends using `BigDecimal(String)` instead of `BigDecimal(double)`
* For more details consult http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
/**
* Returns the value of this [Float] number as a [BigDecimal].
*
* The number is converted to a string and then the string is converted to a [BigDecimal].
*
* @param mathContext specifies the precision and the rounding mode.
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Double.toBigDecimal(): BigDecimal = BigDecimal(this.toString())
public inline fun Float.toBigDecimal(mathContext: MathContext): BigDecimal = BigDecimal(this.toString(), mathContext)
/**
* Returns the value of this [Double] number as a [BigDecimal].
*
* The number is converted to a string and then the string is converted to a [BigDecimal].
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Double.toBigDecimal(): BigDecimal = BigDecimal(this.toString())
/**
* Returns the value of this [Double] number as a [BigDecimal].
*
* The number is converted to a string and then the string is converted to a [BigDecimal].
*
* @param mathContext specifies the precision and the rounding mode.
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Double.toBigDecimal(mathContext: MathContext): BigDecimal = BigDecimal(this.toString(), mathContext)
@@ -1,3 +1,19 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("MathKt")
@file:kotlin.jvm.JvmVersion
@@ -6,6 +22,7 @@ package kotlin
import java.math.BigInteger
import java.math.BigDecimal
import java.math.MathContext
/**
* Enables the use of the `+` operator for [BigInteger] instances.
@@ -58,45 +75,67 @@ public inline operator fun BigInteger.inc(): BigInteger = this.add(BigInteger.ON
@kotlin.internal.InlineOnly
public inline operator fun BigInteger.dec(): BigInteger = this.subtract(BigInteger.ONE)
/** Inverts the bits including the sign bit in this value. */
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun BigInteger.inv(): BigInteger = this.not()
/** Performs a bitwise AND operation between the two values. */
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.and(other: BigInteger): BigInteger = this.and(other)
/** Performs a bitwise OR operation between the two values. */
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.or(other: BigInteger): BigInteger = this.or(other)
/** Performs a bitwise XOR operation between the two values. */
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.xor(other: BigInteger): BigInteger = this.xor(other)
/** Shifts this value left by [bits]. */
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.shl(n: Int): BigInteger = this.shiftLeft(n)
/** Shifts this value right by [bits], filling the leftmost bits with copies of the sign bit. */
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline infix fun BigInteger.shr(n: Int): BigInteger = this.shiftRight(n)
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun String.toBigInteger(): BigInteger = BigInteger(this)
/**
* Returns the value of this [Int] number as a [BigInteger].
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Int.toBigInteger(): BigInteger = BigInteger.valueOf(this.toLong())
/**
* Returns the value of this [Long] number as a [BigInteger].
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun Long.toBigInteger(): BigInteger = BigInteger.valueOf(this)
/**
* Returns the value of this [BigInteger] number as a [BigDecimal].
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun BigInteger.toBigDecimal(): BigDecimal = BigDecimal(this)
public inline fun BigInteger.toBigDecimal(): BigDecimal = BigDecimal(this)
/**
* Returns the value of this [BigInteger] number as a [BigDecimal]
* scaled according to the specified [scale] and rounded according to the settings specified with [mathContext].
*
* @param scale the scale of the resulting [BigDecimal], i.e. number of decimal places of the fractional part.
* By default 0.
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun BigInteger.toBigDecimal(scale: Int = 0, mathContext: MathContext = MathContext.UNLIMITED): BigDecimal =
BigDecimal(this, scale, mathContext)