如何将双重值传递给Android中的textview [英] How to pass double value to a textview in Android

查看:126
本文介绍了如何将双重值传递给Android中的textview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中,我将textview的文本设置为计算的答案,该计算是 double 数据类型。

In Android, I am setting the text of a textview to be the answer to a calculation which is a double data type.

当double值的值为5时,textview接收字符串: 5.0

When the double value has a value of 5, the textview receives the string: 5.0.

代码:

double result = number1/number2;
String finalresult = new Double(result).toString();
textView1.setText(finalresult);

如何格式化textView文本,以便以一致的格式显示双重值? p>

How do I format the textView text so that it shows the double value in a consistent format?

推荐答案

在Android中,为textView分配一个double:

使用Double.toString:

Use Double.toString:

result = number1/number2    
String stringdouble= Double.toString(result);
textview1.setText(stringdouble));

或者您可以使用NumberFormat:

or you can use the NumberFormat:

Double result = number1/number2;
NumberFormat nm = NumberFormat.getNumberInstance();
textview1.setText(nm.format(result));

强制精度为3个单位:

private static DecimalFormat REAL_FORMATTER = new DecimalFormat("0.###");
textview1.setText(REAL_FORMATTER.format(result));

这篇关于如何将双重值传递给Android中的textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆