Convert Numbers to Chinese Characters in Java and Groovy: Multiple Approaches

When test account usernames can no longer contain digits, this guide shows how to transform numeric IDs into Chinese characters using several Java static methods and a Groovy implementation, covering basic loops, parsing, Java 8 streams, and functional style conversions.

FunTester
FunTester
FunTester
Convert Numbers to Chinese Characters in Java and Groovy: Multiple Approaches

Background

In a testing environment, usernames are used to label accounts. A recent policy change disallows digits in the username field, requiring numeric identifiers to be converted into Chinese characters for marking purposes.

Java Implementations

Five static methods are provided, each demonstrating a different technique for converting an int to its Chinese numeral representation.

private static String change1(int n) { /* loop with nested for‑loop */ }
change1

builds a character array of Chinese digits, iterates over the string form of the number, and appends the matching Chinese character.

private static String change2(int n) { /* parse each char */ }
change2

converts each character to an integer with Integer.parseInt and looks up the corresponding Chinese digit.

private static String change3(int n) { /* IntStream range */ }
change3

uses IntStream.range to iterate over the string length and appends the mapped Chinese digit.

private static String change4(int n) { /* IntStream with mapToObj */ }
change4

creates a stream of indices, maps each to the appropriate Chinese character, and joins them.

private static String change5(int n) { /* Arrays.asList stream */ }
change5

converts the number to a char array, streams it, maps each char to a Chinese digit, and joins the result.

Groovy Implementation

Because the project uses Groovy scripts, equivalent functions are provided.

static String[] chineses = ["零","一","二","三","四","五","六","七","八","九"]
static String[] capeChineses = ["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"]
/** Convert int to Chinese numeral, pad to three digits */
static String getChinese(int i) { ... }
/** Convert int to Chinese capital numeral */
static String getCapeChinese(int i) { ... }

The Groovy version leverages collection methods like collect and join to perform the conversion succinctly.

Conclusion

These examples illustrate several ways to replace numeric characters with Chinese equivalents in both Java and Groovy, offering choices ranging from explicit loops to modern stream APIs, and demonstrate that script languages like Groovy can simplify such transformations.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendprogrammingString Manipulationcode-snippetchinese-numbers
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.