如何仅使用整数值或仅使用十进制值显示变量和?

时间:2017-11-28 16:38:05

标签: java android

正如您在代码中看到的,我可以告诉Math.floor变量的值是Decimal还是Integer(如果有更简单的方法请告诉我)。 如果我总和10 + 10它给我20.0。我想要的是如果我的总和是整数我不想显示小数“.0”,但如果它是十进制我想要显示它们(就像我在我的代码中显示)

    public void OnButtonClick(View ver) {

        //Atribuir as caixas de texto a varariavel
        EditText et1 = findViewById(R.id.et1);
        EditText et2 = findViewById(R.id.et2);
        TextView tv1 = findViewById(R.id.tv1);

        float input1;
        float input2;
        //Atribui o conteudo das ET para as variaveis e faz/apresenta a soma
        try {
            input1 = Float.parseFloat(et1.getText().toString());
            input2 = Float.parseFloat(et2.getText().toString());
            float sum = input1 + input2;

            if (sum >  Math.floor(sum)){
                //set text of tv1 with decimals here
                Toast.makeText(getApplicationContext(), "Decimal", Toast.LENGTH_SHORT).show();
            }
            else{
                //set text of tv1 with only integers here
                Toast.makeText(getApplicationContext(), "Integer", Toast.LENGTH_SHORT).show();
            }

            tv1.setText(String.valueOf(sum));

        }
        catch(NumberFormatException ex) {
            //Se estiver vazio msg de erro
            Toast.makeText(getApplicationContext(), "Preencha os campos", Toast.LENGTH_SHORT).show();

        }

    }
}

1 个答案:

答案 0 :(得分:0)

问题是您正在调用String.valueOf(float),它将返回带点的数字。试试这个:

f = figure;
ax = axes ( 'parent', f );
A = [10 5 20 8];
h = bar3(ax, 1:4, A );
xlabel('x'); 
ylabel('y');
% create some new positions for the xdata
index = randperm(4);
% the xdata is blocks on 6x4 coordinates
start = 1;
for ii=1:4
  finish = start+5;
  % for each block of data update the x co-ordinate
  %   this will "move" if along the x-axis
  h.XData(start:finish,:)=h.XData(start:finish,:)+index(ii);
  start = finish+1;
end
% Update the xlim of the axes to display them
ax.XLim = [0 5];