首页 > 其他分享 >9.14

9.14

时间:2023-09-14 21:22:45浏览次数:32  
标签:BigDecimal 9.14 0.01 System 0.05 println out

消除double类型计算出现精度损失的方法

package BigDecimalTest;
import java.math.BigDecimal;

public class TestBigDecimal
{
    public static void main(String[] args) 
    {
        BigDecimal f1 = new BigDecimal("0.05");
        BigDecimal f2 = BigDecimal.valueOf(0.01);
        BigDecimal f3 = new BigDecimal(0.05);
        System.out.println("下面使用String作为BigDecimal构造器参数的计算结果:");
        System.out.println("0.05 + 0.01 = " + f1.add(f2));
        System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
        System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
        System.out.println("0.05 / 0.01 = " + f1.divide(f2));
        System.out.println("下面使用double作为BigDecimal构造器参数的计算结果:");
        System.out.println("0.05 + 0.01 = " + f3.add(f2));
        System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
        System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
        System.out.println("0.05 / 0.01 = " + f3.divide(f2));
    }
}

 value()可以将小括号内的内容转为字符串类型

package Doubletest;
public class TestDouble {

    public static void main(String args[]) {
        System.out.println("0.05 + 0.01 = " + (0.05 + 0.01));
        System.out.println("1.0 - 0.42 = " + (1.0 - 0.42));
        System.out.println("4.015 * 100 = " + (4.015 * 100));
        System.out.println("123.3 / 100 = " + (123.3 / 100));
    }
}

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

生成界面实现两数相加输出

package AdditionTest;
// An addition program 

import javax.swing.JOptionPane;  // import class JOptionPane

public class Addition {
   public static void main( String args[] )
   {
      String firstNumber,   // first string entered by user
             secondNumber;  // second string entered by user
      int number1,          // first number to add
          number2,          // second number to add
          sum;              // sum of number1 and number2

      // read in first number from user as a string
      firstNumber =
         JOptionPane.showInputDialog( "Enter first integer" );

      // read in second number from user as a string
      secondNumber =
         JOptionPane.showInputDialog( "Enter second integer" );

      // convert numbers from type String to type int
      number1 = Integer.parseInt( firstNumber ); 
      number2 = Integer.parseInt( secondNumber );

      // add the numbers
      sum = number1 + number2;

      // display the results
      JOptionPane.showMessageDialog(
         null, "The sum is " + sum, "Results",
         JOptionPane.PLAIN_MESSAGE );

      System.exit( 0 );   // terminate the program
   }
}

 

 

 

标签:BigDecimal,9.14,0.01,System,0.05,println,out
From: https://www.cnblogs.com/xuxingkai/p/17700086.html

相关文章

  • 9.14闲话
    我草我今天真几把难受。感冒了。一天已经喝了三包板蓝根了现在一点好转的迹象都没有......
  • 9.14总结
    1.今天实现了IDEA连接到虚拟机的数据库。2.完成了对虚拟机中的数据进行java增删改查的操作。遇到的问题(已解决):1.IDEA中一直连接不上虚拟机,原因是我忘记在Windows中配置hadoop的系统环境变量导致的。2.IDEA识别不到虚拟机的主机号,原因是在Windows的host文件中没有对虚拟机进行......
  • 9.14每日总结
    启动元数据管理服务(必须启动,否则无法工作) 前台启动:bin/hive--servicemetastore 后台启动:nohupbin/hive--servicemetastore>>logs/metastore.log2>&1& 启动客户端HiveShell方式(可以直接写SQL):  bin/hiveHiveThriftServer方式(不可直接写SQL,需......
  • 23.9.14——改进代码
    importjava.util.Scanner;publicclassMain{  publicstaticvoidmain(String[]args){    intn;    Scannerru=newScanner(System.in);    WarehouseManagementtool=newWarehouseManagement();    while(true){  ......
  • 2023.9.14-每日总结
    今天,我完成了软件需求与分析课堂测试02–业务需求。软件需求与分析课堂测试02–业务需求  ----------------------------------------------------------------------根据下列描述,说明新的直接销售和财务处理系统的业务需求有哪些?EspeciallyforYouJewelers是大学......
  • 9.14每日总结
    软件需求与分析课堂测试02–业务需求 班级:信2105-3班 学号:20214001姓名:张若谷----------------------------------------------------------------------根据下列描述,说明新的直接销售和财务处理系统的业务需求有哪些?EspeciallyforYouJewelers是大学城的一个小珠宝......
  • 2023.9.14日报
    昨天遇到了启动beeline连接hive2报错的问题显示的是拒绝连接,一开始我以为是用户名密码问题,但是再三确认后发现并不是之后查找了site.xml文件,发现配置文件也没有问题,在寻找了很久之后,躺在床上无意间打开了解决方法这个问题来自于我的异常关闭,这也给了我教训,以后使用虚拟机的时候......
  • 9.14 泛型的基本定义
    demo1classPoint<T>{//T属于类型标记,可以设置多个标记privateTx;privateTy;publicvoidsetX(Tx){this.x=x;}publicvoidsetY(Ty){this.y=y;}publicTgetX(){returnthis.x;}publicT......
  • ch9.14
    #include<stdio.h>#include<string.h>intfun(chara[],intl);intmain(){intl; chara[1000]; gets(a);l=strlen(a);  fun(a,l); return0;}intfun(chara[......
  • CentOS 7.9 安装 Consul_1.9.14
    一、CentOS7.9安装Consul_1.9.14地址https://developer.hashicorp.com/consulhttps://github.com/hashicorp/consul wget下载wgethttps://releases.hashic......