E209: Format Interpolation Error
This error occurs when there is a type mismatch or invalid format specifier in an f"" string interpolator. The f interpolator uses Java's Formatter syntax and requires that the format specifiers match the types of the interpolated values.
Example
def example =
val s = "hello"
f"$s%d"
Error
-- [E209] Interpolation Error: example.scala:3:5 -------------------------------
3 | f"$s%d"
| ^
| Found: (s : String), Required: Int, Long, Byte, Short, BigInt
Solution
Use the correct format specifier for the value's type. For strings, use %s:
def example =
val s = "hello"
f"$s%s"
Common Format Specifiers
Here are some common format specifiers and their expected types:
| Specifier | Expected Type | Example |
|---|---|---|
%s |
Any (converted to String) | f"$name%s" |
%d |
Int, Long, Byte, Short, BigInt | f"$count%d" |
%f |
Double, Float, BigDecimal | f"$price%f" |
%x |
Int, Long, Byte, Short, BigInt | f"$hex%x" |
%c |
Char, Byte, Short, Int | f"$char%c" |
%b |
Boolean (or any for null check) | f"$flag%b" |
%e |
Double, Float, BigDecimal | f"$scientific%e" |
Example with Numeric Formatting
def formatNumbers =
val price = 19.99
val count = 42
f"Price: $$$price%.2f, Count: $count%04d"
In this article