본문 바로가기
Android/메모

[Android] 데이터바인딩 사용 시 숫자에 콤마(comma) 넣기

by ESHC 2022. 4. 5.

1000이상의 수 경우 3자리 마다 콤마를 넣어야 하는 경우가 많다.

예 ) 123,456,789 등

 

strings.xml에 아래와 같이 추가한다.

<resources>
    ...
    <string name="comma_number">%,d</string>
    ...
</resources>

 

이후 사용하려는 TextView에서 string 포맷을 사용해준다. 

<androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{@string/comma_number(viewModel.number)}" />

 

 

아래는 %,d원 을 사용한 예시

참고

https://stackoverflow.com/questions/3672731/how-can-i-format-a-string-number-to-have-commas-and-round

 

How can I format a String number to have commas and round?

What is the best way to format the following number that is given to me as a String? String number = "1000500000.574" //assume my value will always be a String I want this to be a String with the...

stackoverflow.com

https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

 

Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated

docs.oracle.com

 

댓글