- 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
推薦閱讀
- Learning Cython Programming
- JavaScript 網頁編程從入門到精通 (清華社"視頻大講堂"大系·網絡開發視頻大講堂)
- Java 9 Programming Blueprints
- 基于免疫進化的算法及應用研究
- C語言程序設計
- Python深度學習:基于TensorFlow
- 青少年信息學競賽
- Microsoft Azure Storage Essentials
- Terraform:多云、混合云環境下實現基礎設施即代碼(第2版)
- 基于ARM Cortex-M4F內核的MSP432 MCU開發實踐
- HTML+CSS+JavaScript編程入門指南(全2冊)
- 軟件工程基礎與實訓教程
- Java并發編程:核心方法與框架
- Julia數據科學應用
- Clojure Web Development Essentials