本文介紹了如何在Android中打印帶有兩位小數的雙精度數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
也許這是一個愚蠢的問題,但如果不是創建方法,我無法猜測如何解決它.也許有一種自然的方式"可以做到這一點,例如在 C 中.問題來了:
Maybe this is a silly question, but I cannot guess how to solve it if it's not creating a method. Maybe there's a "natural way" to do it, like in C for example. Here's the problem:
我有一個變量:
double a;
我只想用 2 或 3 位小數顯示它.當我嘗試展示它時:
And I want to show it only with 2 or 3 decimals. When I try to show it:
Text.setText("Value of a: " + String.valueOf(a));
它給出了類似的東西:
a 的值:5.234966145
Value of a: 5.234966145
我只想
a 的值:5.23
在不改變 a 的實際值的情況下,它顯示的是近似數,但適用于實數.
Without changing the real value of a so it shows the approximate number but works with the real number.
推薦答案
yourTextView.setText(String.format("Value of a: %.2f", a));
這篇關于如何在Android中打印帶有兩位小數的雙精度數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!