how to right justify with $ character

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



how to right justify with $ character



I'm trying to right justify a value (float) with dollar sign. But I'm justifying the value not the dollar sign. So how can I justify the dollar sign as well.


println(f"$$$10.5340%40.2f")
$ 10.53



Thanks




5 Answers
5



Do it in two steps:



Something like that:


println(f"$f"$$$10.5340%.2f"%43s")



Result (total width independent of the number 10.5340):


10.5340


$10.53



Example with many different numbers:


for (n <- List(1234.567, 0.33, 1.0, 42.0, 45.2))
println(f"$f"$$$n%.2f"%43s")



Results in:


$1234.57
$0.33
$1.00
$42.00
$45.20



I hope it's a joke application - you aren't really counting real money using doubles, are you?


double





Thanks, I'm just practicing
– jibril12
Aug 8 at 12:05



Here's one approach using string interpolation on java.text.NumberFormat.getCurrencyInstance:


import java.text.NumberFormat.getCurrencyInstance

val amount1 = f"$getCurrencyInstance.format(10.5340)%40s"
val amount2 = f"$getCurrencyInstance.format(100.5340)%40s"
val amount3 = f"$getCurrencyInstance.format(1000.5340)%40s"

// amount1: String = " $10.53"
// amount2: String = " $100.53"
// amount3: String = " $1,000.53"



In case it's the currency for a different country, say, France:


import java.util.Currency, Locale

val currInstance = getCurrencyInstance

currInstance.setCurrency( Currency.getInstance(new Locale("fr", "FR")) )

val amount4 = f"$currInstance.format(123.456)%40s"
// amount4: String = " EUR123.46"



Try this:


printf("%40c%.2f%n", '$', 10.5340)





That looks strange, because the dots aren't vertically aligned. Try it out with for (n <- List(1234.567, 0.33, 1.0, 42.0, 45.2)) printf("%40c%.2f%n", '$', n)
– Andrey Tyukin
Aug 7 at 20:43


for (n <- List(1234.567, 0.33, 1.0, 42.0, 45.2)) printf("%40c%.2f%n", '$', n)



If it's important that decimal places would align for any number (i.e. padding so that decimal dot is 40 characters from the left, and not padding with 40 characters regardless of number of digits), I can't see how this can be done directly with Scala's formatter - but it can be achieved with the help of Java's DecimalFormat:


DecimalFormat


scala> import java.text._;
import java.text._

scala> println(f"$new DecimalFormat("$###.##").format(10.5340)%40s")
$10.53

scala> println(f"$new DecimalFormat("$###.##").format(1000.5340)%40s")
$1000.53



Code :


println(f"$"$"%40s$10.5340%.2f")



Output :



$10.53





but this would always add 40 spaces, even if the number has more digits - so decimal digits won't align (e.g. if you have 10.534 and 100.534)
– Tzach Zohar
Aug 7 at 19:37






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard