我对用户输入的EditText场。用户可以在按钮上输入值高达十位数number.when点击,我需要显示双precision这个数字的日志[数学日志]值。
我下面showned。结果如果我输入XXXXXXXXXX [考虑以此为号]结果输出必须看起来像这样yy.yy。
包com.example.logvalue;进口android.os.Bundle;进口android.app.Activity;进口android.view.View;进口android.view.View.OnClickListener;进口android.widget.Button;进口android.widget.EditText;进口android.widget.Toast;公共类主要活动扩展{ @覆盖 保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main); 编辑的EditText =(EditText上)findViewById(R.id.editText1); 最终诠释野应=的Integer.parseInt(edit.getText()的toString()); 按钮B1 =(按钮)findViewById(R.id.button1); b1.setOnClickListener(新OnClickListener(){ 公共无效的onClick(查看为arg0){ Toast.makeText(Main.this,的String.format(%2F,Math.log10(野应)),Toast.LENGTH_LONG).show(); } }); }}
解决方案
使用的String.format(%2F,Math.log10(编号))
语法。String类具有这种格式的功能正是这些。
我假设你想用的10个碱基对数。
说明:的String.format
是从C传统的的printf
的Java的equavalient ++。在%
符号是指有一些东西被从参数列表中获取,点意味着我们要指定的小数位数,因此我们把2点后。希望我帮助。
I have a edittext field for users input. user can enter values up to ten digit number.when clicking on the button, i need to show the log [ mathematics log ] value of that number in double precision.
That i showned below. If i input xxxxxxxxxx [ consider this as number ] The output must look like this yy.yy.
package com.example.logvalue;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText edit = (EditText) findViewById(R.id.editText1);
final int noo = Integer.parseInt(edit.getText().toString());
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(Main.this, String.format("%.2f", Math.log10(noo)) , Toast.LENGTH_LONG).show();
}
});
}
}
解决方案
Use the String.format("%.2f", Math.log10(number))
syntax.
The String class has this format function exactly for these.
I assume you want logarithm with base of 10.
Explanation: String.format
is the Java-equavalient of the traditional printf
from C++. The %
symbol means there is something to be fetched from the argument list, the point means that we want to specify the number of decimals, and hence we put a 2 after that point.
Hope I helped.