本文介绍了浮动precision显示(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图做一个程序,它需要一些用户输入,运行一些计算和输出答案。我的问题是这样的回答有时是许多小数位长,这会引起一些审美和布局问题。我只需要显示4价值的数据小数。反正在输出时间限制这些号码的precision? (该号码存储在彩车,我编程为Android。)
I am trying to make a program that takes some user input, runs a few calculations and outputs the answer. My problem is that this answer is sometimes many decimal places long which is causing some aesthetic and layout problems. I only need to display 4 decimal places worth of data. Is there anyway to limit the precision of these numbers at output time? (The Numbers are stored in floats and I'm programming for Android.)
推荐答案
您可以将浮动使用格式化到4位小数的String.format
。
You can format a float to 4 decimal places using String.format
.
例如:
String result = String.format("%.4f", theNumber);
另请参阅:
在Java中
- How to nicely format floating numbers to String without unnecessary decimal 0?
- String.format(format, args)
- Format strings in Java
这篇关于浮动precision显示(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!