Sky Archive

Java/Thymeleaf

[Thymeleaf] number formatting Method, 간단한 숫자 포맷 지정

Anchovy ʕ-᷅ᴥ-᷄ʔ 2022. 3. 31. 14:03

안녕하세요.🐱‍🐉

Thymeleaf를 사용하다 보면 숫자 값을 특정 포맷에 맞춰 표시해야 할 일이 있는데 (ex. 정수의 최소 자릿수를 지정)

Numbers Method에 대해 간단히 정리해보려고 해요.

 

Thymeleaf는 서버사이드 자바 템플릿 엔진의 한 종류로 지원하는 유틸리티 메소드는 Numbers 외에도 Dates, Arrays, Lists, Sets, Maps, 등 다양하게 있어요.

-> https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#appendix-b-expression-utility-objects

 


#numbers

- integer numbers 형식

/* 
 * 최소 정수 자릿수 설정
 *
 * #numbers.formatInteger(표시 값, 최소 자릿수)
 *
 * 표시 값이 최소 자릿수를 넘어가면 전체 값 모두 표시, 소수점은 표시되지 않고 버려짐
 */
${#numbers.formatInteger(num,3)}
// ex. num = 1 / output : 001
${#numbers.arrayFormatInteger(numArray,3)}
${#numbers.listFormatInteger(numList,3)}
${#numbers.setFormatInteger(numSet,3)}


/* 
 * 최소 정수 자릿수와 천 단위 구분자 설정
 *
 * #numbers.formatInteger(표시 값, 최소 자릿수, 천 단위 구분자)
 *
 * 최소 자릿수를 지정하지 않으려면 최소 자릿수에 1을 사용해야 앞에 0이 채워지지 않음.
 * 'POINT', 'COMMA', 'WHITESPACE', 'NONE' or 'DEFAULT' (by locale).
 */
${#numbers.formatInteger(num,4,'COMMA')}
// ex. num = 12 / output : 0,012
${#numbers.arrayFormatInteger(numArray,3,'COMMA')}
${#numbers.listFormatInteger(numList,3,'COMMA')}
${#numbers.setFormatInteger(numSet,3,'COMMA')}

 

- decimal numbers 형식

/*
 * 정수, 소수 최소 자릿수 설정
 *
 * #numbers.formatDecimal(표시 값, 최소 정수 자릿수, 최소 소수 자릿수)
 *
 */
${#numbers.formatDecimal(num,3,2)}
// ex. num = 12.3 / output : 012.30
${#numbers.arrayFormatDecimal(numArray,3,2)}
${#numbers.listFormatDecimal(numList,3,2)}
${#numbers.setFormatDecimal(numSet,3,2)}


/*
 * 정수, 소수 최소 자릿수와 정수 구분자 설정
 *
 * #numbers.formatDecimal(표시 값, 최소 정수 자릿수, 최소 소수 자릿수, 정수 구분자)
 *
 */
${#numbers.formatDecimal(num,3,2,'COMMA')}
// ex. num = 12.3 / output : 012,30
${#numbers.arrayFormatDecimal(numArray,3,2,'COMMA')}
${#numbers.listFormatDecimal(numList,3,2,'COMMA')}
${#numbers.setFormatDecimal(numSet,3,2,'COMMA')}


/*
 * 정수, 소수 최소 자릿수와 천 단위 구분자 설정
 *
 * #numbers.formatDecimal(표시 값, 최소 정수 자릿수, 천 단위 구분자, 최소 소수 자릿수, 정수 구분자)
 *
 */
${#numbers.formatDecimal(num,5,'COMMA',2,'POINT')}
// ex. num = 1234.5 / output : 01,234.50
${#numbers.arrayFormatDecimal(numArray,3,'POINT',2,'COMMA')}
${#numbers.listFormatDecimal(numList,3,'POINT',2,'COMMA')}
${#numbers.setFormatDecimal(numSet,3,'POINT',2,'COMMA')}

 

- 통화 형식

/* 
 * =====================
 * Formatting currencies
 * =====================
 */
${#numbers.formatCurrency(num)}
${#numbers.arrayFormatCurrency(numArray)}
${#numbers.listFormatCurrency(numList)}
${#numbers.setFormatCurrency(numSet)}

 

- 퍼센트 형식

/* 
 * ======================
 * Formatting percentages
 * ======================
 */
${#numbers.formatPercent(num)}
${#numbers.arrayFormatPercent(numArray)}
${#numbers.listFormatPercent(numList)}
${#numbers.setFormatPercent(numSet)}

/* 
 * 정수, 소수 최소 자릿수 설정 후 퍼센트 설정
 */
${#numbers.formatPercent(num, 3, 2)}
${#numbers.arrayFormatPercent(numArray, 3, 2)}
${#numbers.listFormatPercent(numList, 3, 2)}
${#numbers.setFormatPercent(numSet, 3, 2)}

 

- 정수 시퀀스 생성 유틸리티

/*
 * ===============
 * Utility methods
 * ===============
 *
 * x에서 y까지 정수 시퀀스 생성
 */
${#numbers.sequence(from,to)}
${#numbers.sequence(from,to,step)}

 

 

 

 

 

 

 

 

 

 

ref. https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html