How to change the decimal separator of DecimalFormat from comma to dot/point?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
DecimalFormat uses locale-aware symbols, so decimal separators may appear as comma or dot depending on locale defaults. If your output must always use a dot, set DecimalFormatSymbols explicitly instead of relying on runtime locale behavior. This keeps logs, exports, and API payload formatting consistent.
Why Separator Changes by Locale
Java number formatting follows locale conventions. In many European locales, comma is decimal separator and dot is grouping separator.
This often prints using comma decimals under German locale settings.
Force Dot Decimal with DecimalFormatSymbols
Create symbols, set decimal separator, and assign to formatter.
This is the safest way to guarantee output style regardless of default locale.
Pattern Versus Symbols
The pattern controls digit layout, not locale symbols by itself. A dot in pattern does not force dot in output when locale symbols differ.
Without custom symbols, decimal output still follows formatter locale.
Formatting for APIs and Data Exchange
When numbers are serialized for APIs, avoid locale-dependent formatting unless contract requires it. For machine-to-machine payloads, use predictable decimal representation.
For JSON generation, prefer numeric values in serializers rather than formatted strings when possible.
Parsing Inputs with Custom Symbols
If you also parse values with dot decimal, align parse symbols with output symbols.
Mismatch between parse and format configuration is a frequent source of data bugs.
Thread Safety Reminder
DecimalFormat is not thread-safe. Do not share one mutable instance across threads without protection.
Preferred options:
- create per-use instance
- store in thread-local
- use immutable formatting strategies where possible
In web services, request-scoped creation is usually simplest and safe.
Testing Formatting Contracts
Add tests for locale-sensitive code paths by setting explicit locale in tests.
Avoid tests that depend on machine default locale, because CI environments may differ.
Working with Locale.ROOT and Explicit Symbols
If your goal is locale-neutral formatting for logs, metrics, or technical exports, combine an explicit pattern with symbols derived from a stable locale such as Locale.US or Locale.ROOT. This avoids accidental changes when servers run in different regional settings. Many teams standardize on one formatting profile for storage and another for end-user display to avoid mixing machine-readable and user-facing requirements.
Document one canonical formatting policy for backend systems and apply it consistently.
For user interfaces, keep formatting localizable and avoid forcing technical output style in visible UI components. A dual strategy where storage uses one stable machine format and presentation uses locale-aware formatting usually prevents both parsing bugs and user confusion.
Common Pitfalls
- Assuming pattern strings alone force dot decimal output.
- Relying on system default locale and getting different output across environments.
- Forgetting to align parse symbols with format symbols.
- Sharing one
DecimalFormatinstance across threads. - Formatting API payload numerics as locale-specific strings unnecessarily.
Summary
DecimalFormatfollows locale symbols unless overridden.- Use
DecimalFormatSymbolsto force dot decimal output. - Keep parsing and formatting symbols consistent.
- Avoid thread-shared mutable formatter instances.
- Test formatting behavior with explicit locale setup.

