Where is the ceil function on scala.math.BigDecimal? - scala

I can't find the ceil function in the places I've looked:
https://duckduckgo.com/?q=scala+bigdecimal+ceil+function&t=ffab&ia=qa
http://www.scala-lang.org/api/current/scala/math/BigDecimal.html
But I do find a ceil method on Double and Float.
I know MathContext can be made to use the ceiling rounding mode, but I can't seem to make it work:
scala> BigDecimal("1.1").round(new MathContext(0, RoundingMode.UP))
res2: scala.math.BigDecimal = 1.1
scala> BigDecimal("1.8").round(new MathContext(0, RoundingMode.UP))
res3: scala.math.BigDecimal = 1.8
scala> BigDecimal("1.8").round(new MathContext(0, RoundingMode.CEILING))
res5: scala.math.BigDecimal = 1.8
scala> BigDecimal("1.8", new MathContext(0, RoundingMode.CEILING))
res6: scala.math.BigDecimal = 1.8
scala> res6.rounded
res8: scala.math.BigDecimal = 1.8
I expected the result of all the operations above to be == BigDecimal(2).
I use a BigDecimal because these are money operations, hence I need exact values.

To answer the title question: there isn't. At least, not in the way that it exists for Double (and it doesn't really exist for Double, either, it's added implicitly).
You are specifying the precision of the MathContext as zero, which means you're saying you want unlimited precision--that is, no rounding will be done. If you want the true ceiling (rounding up to the nearest integer), then you don't want to use BigDecimal#round at all. The first parameter of the MathContext is precision, which is the number of significant digits you want to maintain. Rounding up a number like 1234567 to keep five significant digits would give you 1234600.
To find the ceiling of a number, you want to set the scale, which is the number of digits to maintain after the decimal point. In this case, you'd want a scale of zero.
import scala.math._, BigDecimal._
scala> BigDecimal("3134.1222").setScale(0, RoundingMode.CEILING)
res1: scala.math.BigDecimal = 3135
scala> BigDecimal("1.1").setScale(0, RoundingMode.CEILING)
res2: scala.math.BigDecimal = 2
scala> BigDecimal("1.8").setScale(0, RoundingMode.CEILING)
res3: scala.math.BigDecimal = 2
We can use the enrich my library pattern to add a ceil method to BigDecimal implicitly:
implicit class RichBigDecimal(bd: BigDecimal) {
def ceil: BigDecimal = bd.setScale(0, RoundingMode.CEILING)
}
scala> BigDecimal("1.1").ceil
res6: scala.math.BigDecimal = 2

Related

Precision set to only 1 decimal place only in scala if all zeros in precision

Scala seems to drop all the decimal places and keep just 1 if we convert an Integer to BigDecimal.
How can I tell the compile to preserve the decimal places irrespective of input.
scala> val tmp:BigDecimal = 1.00
tmp: BigDecimal = 1.0
// was expecting 1.00
scala> val tmp:BigDecimal = 1.01
tmp: BigDecimal = 1.01
scala> val tmp:BigDecimal = 1.11
tmp: BigDecimal = 1.11
EDIT 1: Some context on why I am trying to preserve the decimal places. I work in a fintech job, where the decimals upto 3 places is mandatory for all amount fields. The scala api is leveraged by Front-End team to show customer balance, reports etc.
From the ScalaDocs page we learn:
In most cases, the value of the BigDecimal is also rounded to the precision specified by the MathContext. To create a BigDecimal with a different precision than its MathContext, use new BigDecimal(new java.math.BigDecimal(...), mc).
And, indeed, that does appear to work.
import java.math.{BigDecimal=>JBD, MathContext=>JMC}
val a = BigDecimal(2.4) //a: scala.math.BigDecimal = 2.4
a.precision //res0: Int = 2
val b = BigDecimal(new JBD(2.4, new JMC(7))) //b: scala.math.BigDecimal = 2.400000
b.precision //res1: Int = 7
But, unfortunately, the results aren't always consistent.
import java.math.{BigDecimal=>JBD, MathContext=>JMC}
val a = BigDecimal(2.5) //a: scala.math.BigDecimal = 2.5
a.precision //res0: Int = 2
val b = BigDecimal(new JBD(2.5, new JMC(7))) //b: scala.math.BigDecimal = 2.5
b.precision //res1: Int = 2
So it would appear that precision is either set by the MathContext or it is whatever is sufficient to accurately represent the stated value, whichever is smaller. (Warning: supposition from limited observation.)
However, if what you need is a consistent presentation of the value then I suggest we move away from MathContext to StringContext.
val a :BigDecimal = 1
val b :BigDecimal = 2.2
val c :BigDecimal = 3.456
f"$a%.2f" //res0: String = 1.00
f"$b%.2f" //res1: String = 2.20
f"$c%.2f" //res2: String = 3.46 (note the rounding)

scala can't make an add on a long

I'm not able to do an add on long type.
scala or the processor doesn't manage correctly the sign
scala> var i="-1014570924054025346".toLong
i: Long = -1014570924054025346
scala> i=i+92233720368547758L
i: Long = -922337203685477588
scala> var i=9223372036854775807L
i: Long = 9223372036854775807
scala> i=i+5
i: Long = -9223372036854775804
The first test where a negative number doesn't pass to a positive one is a problem for me
I have not fully understood the question, but for the first example, you get the expected result. What happens in the second example, the Long number happens to be the maximum value for a Long (i.e Long.MaxValue) so essentially when you had another positive number, it's overflowing:
scala> Long.MaxValue
res4: Long = 9223372036854775807L
scala> Long.MaxValue + 1
res7: Long = -9223372036854775808L // which is Long.MinValue
scala> Long.MinValue + 4
res8: Long = -9223372036854775804L // which is the result that you get
In other words:
9223372036854775807L + 5
is equivalent to:
Long.MaxValue + 5
which is equivalent to:
Long.MinValue + 4 // because (Long.MaxValue + 1) = Long.MinValue
which is equals to -9223372036854775804L
If you really need to use such big numbers, you might try using BigInt
scala> val x = BigInt(Long.MaxValue)
x: scala.math.BigInt = 9223372036854775807
scala> x + 1
res6: scala.math.BigInt = 9223372036854775808
scala> x + 5
res11: scala.math.BigInt = 9223372036854775812
scala> x + 10
res8: scala.math.BigInt = 9223372036854775817
scala> x * 1000
res10: scala.math.BigInt = 9223372036854775807000
scala> x * x
res9: scala.math.BigInt = 85070591730234615847396907784232501249
scala> x * x * x * x
res13: scala.math.BigInt = 7237005577332262210834635695349653859421902880380109739573089701262786560001
scala>
The documentation on BigInt is rather, err, small. However, i believe that it is basically an infinite precision integer (can support as many digits as you need). Having said that, there will probably at some point be a limit. There is a comment on BigDecimal - which has more documentation - that at about 4,934 digits there might be some deviation between BigDecimal and BigInt.
I will leave it to someone else to work out whether or not x ^ 4 is the value shown above.
Oh, I almost forgot your negative number test, I aligned the sum with the initialisation, to make it easier to visualise that the result appears to be correct:
scala> val x = BigInt("-1014570924054025346")
x: scala.math.BigInt = -1014570924054025346
scala> x + 92233720368547758L
res15: scala.math.BigInt = -922337203685477588
scala>
As for Ints, Longs and similar data types, they are limited in their size due to the number of bits they are constrained to. Int's are typically 32 bit and longs are typically 64 bits.
It is easier to visualise when you look at them in hexadecimal. A signed Byte (at 8 bits) has a maximum positive value of 0x7F (127). When you add one to it, you get 0x80 (-128). This is because we use the "Most Significant Bit" as an indicator of whether the number is positive or negative.
If the same byte was interpreted as unsigned, then 0x7F (127) would still become 0x80 when 1 is added to it. However, since we are interpreting it as unsigned, this would be equivalent to 128. We can keep adding one until we get to 0xFF (255) at which point if we add another 1 we will end up at 0x00 again which is of course 0.
Here are some references that explain this in much more detail:
Wikipedia - Twos complement
Cornell University - what is twos complement
Stack Overflow - what is 2s complement

Multiply a BigDecimal with an Integer

I want to multiply a financial amount with a quantity. I know Scala uses Java's BigDecimal under the hood but the syntax doesn't seem to be the same.
val price = BigDecimal("0.01") // £0.01
val qty = 10
I tried to do this
BigDecimal(price).*(BigDecimal(qty))
But it's a compile error. If I look at the Java SO posts you can pass integer into BigDecimal and then multiply it like this
BigDecimal(price).multiply(BigDecimal(qty))
So how do you do this in Scala? And are there any dangers in losing precision by multiplying a decimal and integer like this? I will need sum a lot of these together as well
You can actually multiply a BigDecimal with an Int using BigDecimal's multiplication operator:
def *(that: BigDecimal): BigDecimal
since the Int you will provide as its parameter will be implicitly converted to a BigDecimal based on:
implicit def int2bigDecimal(i: Int): BigDecimal
You can thus safely multiply your BigDecimal with an Int as if it was a BigDecimal and receive a BigDecimal with no loss in precision:
val price = BigDecimal("0.01")
// scala.math.BigDecimal = 0.01
val qty = 10
// Int = 10
price * qty // same as BigDecimal("0.01").*(10)
// scala.math.BigDecimal = 0.10
You can do this:
val a = 10
val b = BigDecimal(0.1000000000001)
a * b
res0: scala.math.BigDecimal = 1.0000000000010
As you can see you don´t lose precision
The problem is actually this:
BigDecimal(price)
price is already a BigDecimal so the compiler does't know what to do! If you fix this, the first version works. The second version fails because there is no multiply method on BigDecimal.
However, as others have pointed out, the simple solution is just
price*qty

Count number of Strings that can be converted to Int in a List

For example, my input is:
scala> val myList = List("7842", "abf45", "abd", "56")
myList: List[String] = List(7842, abf45, abd, 56)
7842 and 56 can be converted to Int; therefore, my expected output is 2. We can assume that negative integers don't happen, so -67 is not possible.
This is what I have so far:
scala> myList.map(x => Try(x.toInt).getOrElse(-1)).count(_ > -1)
res15: Int = 2
This should work correctly, but I feel like I am missing a more elegant and readable solution, because all I have to do is count number of successes.
I would caution against using exception handling (like Try) in control flow -- it's very slow.
Here's a solution that uses idiomatic Scala collection operations, performs well, and will not count negative numbers:
scala> val myList = List("7842", "abf45", "abd", "56")
myList: List[String] = List(7842, abf45, abd, 56)
scala> myList.count(_.forall(_.isDigit))
res8: Int = 2
EDIT: #immibis pointed out that this won't detect strings of numbers that exceed Integer.MaxValue. If this is a concern, I would recommend one of the following approaches:
import scala.util.Try
myList.count(x => Try(x.toInt).filter(_ >= 0).isSuccess)
or, if you want to keep the performance of my first answer while still handling this edge case:
import scala.util.Try
myList.count(x => x.forall(_.isDigit) && Try(x.toInt).filter(_ >= 0).isSuccess)
This is a bit shorter:
myList.count(x => Try(x.toInt).isSuccess)
Note that this solution will handle any string that can be converted to integer via .toInt, including negative numbers.
You may consider string.matches method with regex as well, to match only positive integers:
val myList = List("7842", "abf45", "abd", "-56")
// myList: List[String] = List(7842, abf45, abd, -56)
myList.count(_.matches("\\d+"))
// res18: Int = 1
If negative integers need to be counted (and take into account possible +/- signs):
myList.count(_.matches("[+-]?\\d+"))
// res17: Int = 2
Starting Scala 2.13 and the introduction of String::toIntOption, we can count items ("34"/"2s3") for which applying toIntOption (Some(34)/None) is defined (true/false):
List("34", "abf45", "2s3", "56").count(_.toIntOption.isDefined) // 2

java.lang.Integer cannot be cast to java.lang.Byte error with Any type in Scala

I can cast Int data to Byte.
scala> 10.asInstanceOf[Byte]
res8: Byte = 10
However with the same value in Any type, the cast raises an error.
scala> val x : Any = 10
x: Any = 10
scala> x.asInstanceOf[Byte]
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Byte
at scala.runtime.BoxesRunTime.unboxToByte(BoxesRunTime.java:98)
at .<init>(<console>:10)
I can cast twice.
scala> val y = x.asInstanceOf[Int]
y: Int = 10
scala> y.asInstanceOf[Byte]
res11: Byte = 10
Are there better ways than this?
In Scala, compiler tries to hide the distinction between primitive types and reference ones (boxed), defaulting to primitives. Sometimes, abstractions leak and you see that kind of problems.
Here, you're pretending that value is Any, which require compiler to fallback to boxed values:
override def set(value:Any) = {
if (check(value.asInstanceOf[Byte])) {
And here, you're not limiting value to be referential, so such casting will be done on primitive types:
10.asInstanceOf[Byte]
In other words:
scala> val x: Any = 10
x: Any = 10
scala> x.asInstanceOf[Byte]
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Byte
at scala.runtime.BoxesRunTime.unboxToByte(BoxesRunTime.java:97)
... 32 elided
scala> val y: Int = 10
y: Int = 10
scala> y.asInstanceOf[Byte]
res4: Byte = 10
To overcome this problem, you probably have to go through an extra conversion to, say, String:
scala> x.toString.toInt
res6: Int = 10
scala> x.toString.toByte
res7: Byte = 10
Try converting to int and then to Byte:
scala> val x : Any = 10
x: Any = 10
scala> x.asInstanceOf[Int].asInstanceOf[Byte]
res1: Byte = 10
Or as Ionuț G. Stan suggested:
scala> x.asInstanceOf[Int].toByte
res4: Byte = 10
Although I cannot explain why this work.
An integer is 32 bits in Java, while a byte is obviously 8 bits. The problem is what bits do you truncate to make an integer a byte? The least significant 24 bits or the most significant 24 bits? The correct answer is in the context of your problem.