Java
String Formatting
Java Programming
Data Conversion
Java Tips

How to go about formatting 1200 to 1.2k in java

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Formatting 1200 as 1.2k is a compact-number formatting problem. The goal is not just to divide by 1000, but to choose a suffix, control rounding, and avoid odd results such as 1000.0k instead of 1M. In Java, the cleanest solution is usually a small utility method unless you already depend on a library that provides compact formatting.

Basic Idea

The common English-language suffixes are:

  • 'k for thousands'
  • 'M for millions'
  • 'B for billions'
  • 'T for trillions'

The algorithm is straightforward:

  1. keep numbers below 1000 unchanged
  2. divide by 1000 until the value fits the current suffix
  3. format with one decimal place when needed
  4. trim trailing .0

A Simple Utility Method

java
1import java.text.DecimalFormat;
2
3public class CompactNumberFormatter {
4    private static final String[] SUFFIXES = {"", "k", "M", "B", "T"};
5
6    public static String format(long value) {
7        if (value == Long.MIN_VALUE) {
8            return format(Long.MIN_VALUE + 1);
9        }
10
11        boolean negative = value < 0;
12        double number = Math.abs((double) value);
13        int suffixIndex = 0;
14
15        while (number >= 1000 && suffixIndex < SUFFIXES.length - 1) {
16            number /= 1000.0;
17            suffixIndex++;
18        }
19
20        DecimalFormat df = new DecimalFormat("0.#");
21        String result = df.format(number) + SUFFIXES[suffixIndex];
22        return negative ? "-" + result : result;
23    }
24
25    public static void main(String[] args) {
26        System.out.println(format(950));
27        System.out.println(format(1200));
28        System.out.println(format(15320));
29        System.out.println(format(1250000));
30        System.out.println(format(-4200));
31    }
32}

That prints values such as 950, 1.2k, 15.3k, 1.3M, and -4.2k.

Handle Rounding Carefully

A common bug appears near thresholds. For example, 999950 might round to 1000.0k, which is not what you want. You normally want 1M.

You can fix that by checking whether rounding pushed the value to 1000 and then promoting it to the next suffix.

java
1import java.text.DecimalFormat;
2
3public class BetterCompactFormatter {
4    private static final String[] SUFFIXES = {"", "k", "M", "B", "T"};
5
6    public static String format(long value) {
7        boolean negative = value < 0;
8        double number = Math.abs((double) value);
9        int suffixIndex = 0;
10
11        while (number >= 1000 && suffixIndex < SUFFIXES.length - 1) {
12            number /= 1000.0;
13            suffixIndex++;
14        }
15
16        if (number >= 999.95 && suffixIndex < SUFFIXES.length - 1) {
17            number /= 1000.0;
18            suffixIndex++;
19        }
20
21        DecimalFormat df = new DecimalFormat("0.#");
22        String text = df.format(number) + SUFFIXES[suffixIndex];
23        return negative ? "-" + text : text;
24    }
25}

That extra threshold check makes the output feel more natural.

If You Need Locale-Aware Formatting

If the exact lowercase k style is not mandatory, you can look at NumberFormat features in newer Java versions or use ICU libraries for locale-aware compact numbers. That matters because abbreviations and separators differ by locale.

Still, for many applications such as dashboards or admin tools, a custom helper is perfectly reasonable because the output format is explicit and stable.

Decide on the Product Rules First

Before writing code, settle these behavior questions:

  • Should 1000 become 1k or 1.0k?
  • Should 1250 become 1.2k or 1.3k?
  • Should negatives keep the suffix, such as -1.2k?
  • Do you need B and T, or only k and M?
  • Is the output always English, or locale-sensitive?

These are presentation decisions, not Java-specific rules. The formatter should reflect the UI requirement, not whatever default rounding happened to be easiest.

Common Pitfalls

  • Dividing by 1000 once and forgetting larger suffixes such as M or B.
  • Printing 1.0k when the UI expects 1k.
  • Letting rounded values produce awkward output such as 1000k.
  • Ignoring negative numbers and zero.
  • Assuming compact formatting rules are universal across locales.

Summary

  • Formatting 1200 to 1.2k is a compact-number formatting task.
  • A small helper method is usually enough in Java.
  • Use suffixes, controlled rounding, and threshold promotion for cleaner output.
  • Decide early whether formatting is English-specific or locale-aware.
  • Test edge cases near 1000, 1_000_000, and negative values.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.