- 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
推薦閱讀
- C++程序設(shè)計(jì)教程
- Learn TypeScript 3 by Building Web Applications
- GAE編程指南
- TestNG Beginner's Guide
- Vue.js 3.0源碼解析(微課視頻版)
- 云原生Spring實(shí)戰(zhàn)
- 你必須知道的204個(gè)Visual C++開(kāi)發(fā)問(wèn)題
- MATLAB 2020從入門(mén)到精通
- Mastering ROS for Robotics Programming
- Python Data Science Cookbook
- 深入淺出Go語(yǔ)言編程
- 網(wǎng)絡(luò)數(shù)據(jù)采集技術(shù):Java網(wǎng)絡(luò)爬蟲(chóng)實(shí)戰(zhàn)
- 深入淺出 HTTPS:從原理到實(shí)戰(zhàn)
- 你好!Java
- 零基礎(chǔ)PHP從入門(mén)到精通