String interpolation number thousands separator doesn't work.

290 views
Skip to first unread message

Ian Wood

unread,
Feb 17, 2015, 6:12:31 AM2/17/15
to scal...@googlegroups.com
Hi,

In the repl the following code to format numbers with a thousands separator works,

scala> val toMakeOption = 29005000
toMakeOption: Int = 29005000

scala>  println(f"created new, took [$toMakeOption%,d]")
created new, took [29,005,000]

The same code when ran via ScalaJS shows this in the console, without the separator.
created new, took [29005000]

Looking for help I found this page
http://stackoverflow.com/questions/3753483/javascript-thousand-separator-string-format which suggests:
which suggests:
parseInt( $yourInt ).toLocaleString()
This does work in the browser javascript console

So, is there an incantation to use to get thousands separators working, or how do I call that javascript from ScalaJS?

Thanks,

Ian


Sébastien Doeraene

unread,
Feb 17, 2015, 7:08:24 AM2/17/15
to Ian Wood, scal...@googlegroups.com
Hi,

You can call the JavaScript call like this:
toMakeOption.asInstanceOf[js.Object].toLocaleString()
(a bit weird, but it'll work)

But I guess we could make 'format' to work. It's just that we don't implement the ',' modifier in https://github.com/scala-js/scala-js/blob/master/javalib/src/main/scala/java/util/Formatter.scala

Cheers,
Sébastien

--
You received this message because you are subscribed to the Google Groups "Scala.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-js+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-js/35b64ab3-720b-4e5b-8b50-ddfc00d97c17%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Udah

unread,
Feb 17, 2015, 9:28:15 AM2/17/15
to scal...@googlegroups.com, theonly...@gmail.com
Thanks Sébastien,

This works as expected. We found that this doesn't work if toMakeOption is of type Long (which was what we were doing), so I've tested this for Int, Long and Double values as below:

val toMakeOption: Long = System.nanoTime() - startForOption
println(f"created new, took [$toMakeOption%,d]ns")

val w = 1234567890.asInstanceOf[js.Object].toLocaleString()
println(s"w[Int] = $w")

val x = toMakeOption.asInstanceOf[js.Object].toLocaleString()
println(s"x[Long] = $x")

val y = toMakeOption.toInt.asInstanceOf[js.Object].toLocaleString()
println(s"y[Int] = $y")

val z = toMakeOption.toDouble.asInstanceOf[js.Object].toLocaleString()
println(s"z[Double] = $z")

and we got the following output in JavaScript console:

created new, took [22799000]ns
w[Int] = 1,234,567,890
x[Long] = 22799000
y[Int] = 22,799,000
z[Double] = 22,799,000

Why doesn't it work for longs do you think?

Thanks

Sébastien Doeraene

unread,
Feb 17, 2015, 9:43:17 AM2/17/15
to Robert Udah, scal...@googlegroups.com, theonly...@gmail.com
Because Longs are an opaque type, so they are not truly JavaScript numbers, unlike all other numeric types in Scala.js. So basically the cast someLong.asInstanceOf[js.Object]  is nonsensical.
If your Longs are not too long (<= 52 bits of precision), you can first convert them to Doubles with .toDouble:
toMakeOption.toDouble.asInstanceOf[js.Object].toLocaleString()

Sébastien

Reply all
Reply to author
Forward
0 new messages