I know that for in Scala.js (cannot use java.text.DecimalFormat) I can write this:
val number = 1.2345
println(f"$x%.2f") // "1.23"
However, this doesn't seem to work:
val decimalPlaces = 2
println(f"$x%.${decimalPlaces}f")
// [error] Missing conversion operator in '%'; use %% for literal %, %n for newline f"$x%.${decimalPlaces}f"
// also doesn't work: (f"$x%." + decimalPlaces + "f").toFloat
How can I achieve a variable decimal precision?
This works
val number = 1.2345
val decimalPlaces = 2
println(("%." + decimalPlaces + "f").format(number))
There is an implicit call to StringLike for format.
I suppose the reason it doesn't work is that nowhere in the expression:
s"$number%.$decimalPlacesf" # DOESN'T WORK
we are providing the order on how should the variables be resolved.
You need to artificially enforce it. Similarly to #nattyddubbs's answer:
val number = 1.2345
val decimalPlaces = 3
val format = s"%.${decimalPlaces}f"
println(format.format(number)) # 1.235
Related
I have an array
val a = "((x1,x2),(y1,y2),(z1,z2))"
I want to parse this into a scala array
val arr = Array(("x1","x2"),("y1","y2"),("z1","z2"))
Is there a way of directly doing this with an expr() equivalent ?
If not how would one do this using split
Note : x1 x2 x3 etc are strings and can contain special characters so key would be to use () delimiters to parse data -
Code I munged from Dici and Bogdan Vakulenko
val x2 = a.getString(1).trim.split("[\()]").grouped(2).map(x=>x(0).trim).toArray
val x3 = x2.drop(1) // first grouping is always null dont know why
var jmap = new java.util.HashMap[String, String]()
for (i<-x3)
{
val index = i.lastIndexOf(",")
val fv = i.slice(0,index)
val lv = i.substring(index+1).trim
jmap.put(fv,lv)
}
This is still suceptible to "," in the second string -
Actually, I think regex are the most convenient way to solve this.
val a = "((x1,x2),(y1,y2),(z1,z2))"
val regex = "(\\((\\w+),(\\w+)\\))".r
println(
regex.findAllMatchIn(a)
.map(matcher => (matcher.group(2), matcher.group(3)))
.toList
)
Note that I made some assumptions about the format:
no whitespaces in the string (the regex could easily be updated to fix this if needed)
always tuples of two elements, never more
empty string not valid as a tuple element
only alphanumeric characters allowed (this also would be easy to fix)
val a = "((x1,x2),(y1,y2),(z1,z2))"
a.replaceAll("[\\(\\) ]","")
.split(",")
.sliding(2)
.map(x=>(x(0),x(1)))
.toArray
I have the following Double in Scala:
val value: Double = 12.34
and get the formatted value, like so:
val formatted = f"$value%1.5f"
But I need to set the number of decimals (above 5) programmatically. I tried this, but it doesn't work:
val dec = 8
val formatted = f"$value%1.decf"
Any ideas?
val value: Double = 12.34
val dec = 8
val formatted = s"%1.${dec}f".format(value) // 12.34000000
You can use the scala BigDecimal with its setScale def then convert to a Double if necessary:
BigDecimal(12.35564126).setScale(5, BigDecimal.RoundingMode.HALF_UP).toDouble
// res0: Double = 12.35564
How about
fmt="%."+n+"f"
fmt.format(12.34)
Too obvious?
What is the simplest/idiomatic way to format percentages in Scala?
I have the following solution but I'm wondering if a more concise way exists:
val value = 0.1456
val s1 = f"the float value is ${value}%.2f"
val s2= s"the percent value is ${java.text.NumberFormat.getPercentInstance.format(value)}"
value: Double = 0.1456
s1: String = the float value is 0.15
s2: String = the percent value is 15%
If you are looking for more of a concise method, the following works and goes along with your initial idea in your code. It is also easy to add on decimal placement without having to resort to the implicit functionality. Obviously these needs to be used a lot a better solution is with the implicit method.
val value = 0.1456
val s2 = val s2 = f"the percent value is ${value*100}%.0f%%"
s2: String = the percent value is 15%
just to give a couple of other runs as well (t show rounding down here):
val value2 = 0.1416
val s3 = val s2 = f"the percent value is ${value2*100}%.0f%%"
s3: String = the percent value is 14%
Example to show adding decimal places:
val s4 = f"the percent value is ${value2*100}%.1f%%"
s4: String = the percent value is 14.2%
You could use the "pimp my library" pattern to add the asPercentage method to Doubles.
implicit class DoubleAsPercentage(d: Double) {
def asPercentage = java.text.NumberFormat.getPercentInstance.format(d)
}
val s2 = s"the percent value is ${value.asPercentage}"
You can use the f string to format the percentage to the relevant decimal places you wish. In this example, it has 4 decimal places however if you only want to return two decimal places, then use .2f in the string below.
To return 3 decimal places, then use .3f etc etc. However if you want more decimal places than suggested; "here they are 4", you will end up with trailing zeros.
val percentage = 51.9938
scala> f"I scored $percentage%.8f%% in my exams"
res164: String = I scored 51.99380000% in my exams
scala> f"I scored $percentage%.2f%% in my exams"
res165: String = I scored 51.99% in my exams
I'm trying to generate a random String, and these are the possibilities I've found:
Random.nextPrintableChar(), which prints letters, numbers, punctuation
Random.alphanumeric.take(size).mkString, which prints letters and numbers
Random.nextString(1), which prints Chinese chars almost every time lol
Random is scala.util.Random
size is an Int
The second option almost does the job, but I need to start with a letter. I found Random.nextPrintableChar() but it also prints punctuation.
What's the solution?
My solution so far was:
val low = 65 // A
val high = 90 // Z
((Random.nextInt(high - low) + low).toChar
Inspired by Random.nextPrintableChar implementation:
def nextPrintableChar(): Char = {
val low = 33
val high = 127
(self.nextInt(high - low) + low).toChar
}
Found a better solution:
Random.alphanumeric.filter(_.isLetter).head
A better solution as jwvh commented: Random.alphanumeric.dropWhile(_.isDigit)
For better control of the contents, select the alphabet yourself:
val alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
def randStr(n:Int) = (1 to n).map(_ => alpha(Random.nextInt(alpha.length))).mkString
Actually the fastest method to generate Random ASCII String is the following
val rand = new Random()
val Alphanumeric = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes
def mkStr(chars: Array[Byte], length: Int): String = {
val bytes = new Array[Byte](length)
for (i <- 0 until length) bytes(i) = chars(rand.nextInt(chars.length))
new String(bytes, StandardCharsets.US_ASCII)
}
def nextAlphanumeric(length: Int): String = mkStr(Alphanumeric, length)
I have written a function in Scala that should calculate the sum of the squares of the digits of a number. Eg: 44 -> 32 (4^2 + 4^2 = 16 + 16 = 32)
Here it is:
def digitSum(x:BigInt) : BigInt = {
var sum = 0
val leng = x.toString.toList.length
var y = x.toString.toList
for (i<-0 until leng ) {
sum += y(i).toInt * y(i).toInt
}
return sum
}
However when I call the function let's say with digitSum(44) instead of 32 I get 5408.
Why is this happening? Does it have to do with the fact that in the list there are Strings? If so why does the .toInt method do not work?
Thanks!
The answer to your questions has been already covered here Scala int value of String characters, have a good read through and you will have more information than required ;)
Also looking at your code, it can benefit more from Scala expressiveness and functional features. The same function can be written in the following manner:
def digitSum(x: BigInt) = x.toString
.map(_.asDigit)
.map(a => a * a)
.sum
In the future try to avoid using mutable variables and standard looping techniques if you could.
When you do toString you're mapping the String to Chars not Ints and then to Ints later. This is what it looks like in the repl:
scala> "1".toList.map(_.toInt)
res0: List[Int] = List(49)
What you want is probably something like this:
def digitSum(x:BigInt) : BigInt = {
var sum = 0
val leng = x.toString.toList.length
var y = x.toString.toList
for (i<-0 until leng ) {
sum += (y(i).toInt - 48) * (y(i).toInt - 48) //Subtract out char base
}
sum
}