如何使数字结果不带小数

时间:2019-10-23 23:24:24

标签: java android numbers decimal rounding

我的代码有问题。我将复制简化的代码,其中包含两个editext和一个textview和按钮。 使用此代码,如果editext1中的值为“ 100”,而其他editext2为“ 60”,我得到的结果为“ 40.0”,并且应该为“ 40”。谢谢

获取代码:

public class MainActivity extends AppCompatActivity {
    EditText number1;
    EditText number2;
    Button Add_button3;
    TextView descr;
    int ans=0;
    private BreakIterator view;

    @SuppressLint("CutPasteId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //
        number1=(EditText) findViewById(R.id.editText_first_no);
        number2=(EditText) findViewById(R.id.editText_second_no);
        Add_button3=(Button) findViewById(R.id.add_button3);
        descr = (TextView) findViewById(R.id.textView_DESC);

        //
        Add_button3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                double num1 = Double.parseDouble(number1.getText().toString());
                double num2 = Double.parseDouble(number2.getText().toString());
                double sump = num1 - num2;

                descr.setText(Double.toString(sump));
            }
        });
    }
}

2 个答案:

答案 0 :(得分:1)

有两个选项,强制转换和使用Math类。

要投射:descr.setText(Integer.toString((int)sump));

这种方法只有一个缺点,就是您正在丢失信息。

因此,如果您抽象的double sump = 5.0 - 3.3;等于:1.7000000000000002,则强制类型转换为'1'。

我认为更好的方法是使用Math类,尤其是使用random()方法,即:

descr.setText(Math.round(sump));

该方法还会删除一些数据,但是会将数字四舍五入为最接近的整数(整数),这是处理类似情况的首选方法。

有关更多信息,请检查:How to convert float to int with Java

答案 1 :(得分:0)

像这样使用PdfReader reader = new PdfReader(new FileInputStream(new File("D:\\ruta\\\\basepdf.pdf"))); Document document = new Document(reader.getPageSize(1),0,0,0,0); BaseFont base =BaseFont.createFont("D:\\ruta\\fuente.ttf", BaseFont.CP1250, BaseFont.EMBEDDED); Font fonts = new Font(base, Float.parseFloat("40"),Font.BOLD, new BaseColor(255,0,0)); Paragraph parrafo= new Paragraph("https://www.youtube.com/", fonts); Image imagen = Image.getInstance("D:\\ruta\\usuariosfondo.jpg"); String base64Data = base64.substring(base64.indexOf(',') + 1); byte[] decoded = Base64.decodeBase64(base64Data.getBytes()); Image imgQr = Image.getInstance(decoded); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter writer=PdfWriter.getInstance(document, baos); document.open(); document.newPage(); parrafo.setAlignment(Element.ALIGN_RIGHT); parrafo.setFont(fonts); ColumnText ct = new ColumnText(writer.getDirectContent()); ct.setSimpleColumn(0,0,550,780); ct.setAlignment(Element.ALIGN_RIGHT); ct.addElement(parrafo); ct.go(); Float width = document.getPageSize().getWidth(); Float height = document.getPageSize().getHeight(); imagen.scaleToFit(width, height); imagen.setAbsolutePosition(0f, 0f); imgQr.setAbsolutePosition(149f, 209f); imgQr.scaleToFit(178.2992f, 178.2992f); document.add(imagen); document.add(imgQr); //document.add(parrafo); document.close(); String p=java.util.Base64.getEncoder().encodeToString(baos.toByteArray());

在科特林

DecimalFormat

在Java中:

DecimalFormat("##.##").format(yourValue)