如何在计算器上用 Int 显示结果?
How to show result with Int on calculator?
我正在制作计算器应用程序。即使我不使用 double,它也会在结果中显示 double。
示例)1+1 = 2.0
但我想要 1+1=2
当然有1.2+1.3=2.5这样的double我想保持double
我应该如何编辑?
我试过这样编辑,但是出现错误。
public void equalsOnClick(View view)
{
Integer result = null;
ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");
try {
result = (int)engine.eval(workings);
} catch (ScriptException e)
{
Toast.makeText(this, "Invalid Input", Toast.LENGTH_SHORT).show();
}
if(result != null)
resultsTV.setText(String.valueOf(result.intValue()));
}
MainActivity
public class MainActivity extends AppCompatActivity {
TextView workingsTV;
TextView resultsTV;
String workings = "";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTextView();
}
private void initTextView()
{
workingsTV = (TextView)findViewById(R.id.workingsTextView);
resultsTV = (TextView)findViewById(R.id.resultTextView);
}
private void setWorkings(String givenValue)
{
workings = workings + givenValue;
workingsTV.setText(workings);
}
public void equalsOnClick(View view)
{
Double result = null;
ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");
try {
result = (double)engine.eval(workings);
} catch (ScriptException e)
{
Toast.makeText(this, "Invalid Input", Toast.LENGTH_SHORT).show();
}
if(result != null)
resultsTV.setText(String.valueOf(result.doubleValue()));
}
public void clearOnClick(View view)
{
workingsTV.setText("");
workings = "";
resultsTV.setText("");
leftBracket = true;
}
}
使用模运算符检查双精度数是否为整数
(结果 %1 ==0)
或 Math.floor 然后检查结果是否改变。
如果是,可以使用Integer.valueOf(result)
Integer 有一个内置的 toString 方法。
发生这种情况是因为您声明了 result
类型 Double
。因此,在您将它的 doubleValue()
转换为 int
并将其设置为 resultsTV
之前,它的 double
值将设置在那里。
按如下方式更改您的方法定义:
public void equalsOnClick(View view) {
Double result = null;
ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");
try {
result = (Double)engine.eval(workings);
if(result != null) {
int intVal = (int)result.doubleValue();
if(result == intVal) {// Check if it's value is equal to its integer part
resultsTV.setText(String.valueOf(intVal));
} else {
resultsTV.setText(String.valueOf(result));
}
}
} catch (ScriptException e) {
Toast.makeText(this, "Invalid Input", Toast.LENGTH_SHORT).show();
}
}
请注意,我还将 resultsTV.setText
移动到 try-catch
块中,以便它仅在 result = (Double)engine.eval(workings)
不抛出异常时执行。
我正在制作计算器应用程序。即使我不使用 double,它也会在结果中显示 double。 示例)1+1 = 2.0
但我想要 1+1=2
当然有1.2+1.3=2.5这样的double我想保持double
我应该如何编辑?
我试过这样编辑,但是出现错误。
public void equalsOnClick(View view)
{
Integer result = null;
ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");
try {
result = (int)engine.eval(workings);
} catch (ScriptException e)
{
Toast.makeText(this, "Invalid Input", Toast.LENGTH_SHORT).show();
}
if(result != null)
resultsTV.setText(String.valueOf(result.intValue()));
}
MainActivity
public class MainActivity extends AppCompatActivity {
TextView workingsTV;
TextView resultsTV;
String workings = "";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTextView();
}
private void initTextView()
{
workingsTV = (TextView)findViewById(R.id.workingsTextView);
resultsTV = (TextView)findViewById(R.id.resultTextView);
}
private void setWorkings(String givenValue)
{
workings = workings + givenValue;
workingsTV.setText(workings);
}
public void equalsOnClick(View view)
{
Double result = null;
ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");
try {
result = (double)engine.eval(workings);
} catch (ScriptException e)
{
Toast.makeText(this, "Invalid Input", Toast.LENGTH_SHORT).show();
}
if(result != null)
resultsTV.setText(String.valueOf(result.doubleValue()));
}
public void clearOnClick(View view)
{
workingsTV.setText("");
workings = "";
resultsTV.setText("");
leftBracket = true;
}
}
使用模运算符检查双精度数是否为整数 (结果 %1 ==0) 或 Math.floor 然后检查结果是否改变。
如果是,可以使用Integer.valueOf(result)
Integer 有一个内置的 toString 方法。
发生这种情况是因为您声明了 result
类型 Double
。因此,在您将它的 doubleValue()
转换为 int
并将其设置为 resultsTV
之前,它的 double
值将设置在那里。
按如下方式更改您的方法定义:
public void equalsOnClick(View view) {
Double result = null;
ScriptEngine engine = new ScriptEngineManager().getEngineByName("rhino");
try {
result = (Double)engine.eval(workings);
if(result != null) {
int intVal = (int)result.doubleValue();
if(result == intVal) {// Check if it's value is equal to its integer part
resultsTV.setText(String.valueOf(intVal));
} else {
resultsTV.setText(String.valueOf(result));
}
}
} catch (ScriptException e) {
Toast.makeText(this, "Invalid Input", Toast.LENGTH_SHORT).show();
}
}
请注意,我还将 resultsTV.setText
移动到 try-catch
块中,以便它仅在 result = (Double)engine.eval(workings)
不抛出异常时执行。