官术网_书友最值得收藏!

  • Learning Scala Programming
  • Vikash Sharma
  • 119字
  • 2021-06-30 19:07:53

f Interpolator

To have something like printf styled formatting in Scala, we can use the f interpolator. We do this by using a f preceding the double quotes of our string, and then within the String we can use one of the format specifiers:

scala> val amount = 100
amount: Int = 100
scala> val firstOrderAmount = f"Your total amount is: $amount%.2f"
firstOrderAmount: String = Your total amount is: 100.00

From the preceding example, it's clear that we use f as a prefix to our string and use $ followed by the expression that includes our format specifier. This works as a formatter for our strings.

A few format specifiers are listed as following:

Format Specifiers
主站蜘蛛池模板: 富源县| 卢龙县| 伊宁市| 忻州市| 舞钢市| 西乡县| 集贤县| 江孜县| 舟曲县| 深州市| 赣榆县| 太湖县| 青州市| 永川市| 绵竹市| 株洲县| 贵溪市| 固阳县| 瑞金市| 华池县| 廉江市| 嵊泗县| 山丹县| 方城县| 四子王旗| 易门县| 沙雅县| 安阳市| 丁青县| 于都县| 云阳县| 新绛县| 大足县| 扬州市| 清新县| 凤山市| 衡阳市| 文山县| 普洱| 进贤县| 通山县|