java怎样解决除法精度_java 除法 精度问题

java怎样解决除法精度_java 除法 精度问题inta=4;intb=3;floatc=(float)a/b;System.out.print(c);//输出:1如果要的到精确的结果,要用下面的方法inta=4;intb=3;floatc=(float)a/(float)b;System.out.print(c);//输出:1.3333334importjava.text.DecimalFormat;public…

大家好,又见面了,我是你们的朋友全栈君。

int a=4;

int b=3;

float c = (float) a/b;

System.out.print(c);//输出:1

如果要的到精确的结果,要用下面的方法

int a=4;

int b=3;

float c = (float) a/(float) b;

System.out.print(c);//输出:1.3333334

import java.text.DecimalFormat;

public class toDouble {

public static void main(String[] args){

//增加三位小数0

DecimalFormat df = new DecimalFormat(“0.000”);

double d = 123;

System.out.println(df.format(d));

//保留4位小数

double d1 = 123.0123456;

String d2 = String.format(“%.4f”, d1);

System.out.println(d2);

int d3=5;

//float d4=d3/100;

String d4 = String.format(“%.3f”, (float)d3/100.00);

System.out.println(d4);

}

}

输出:

123.000

123.0123

0.050

Java除法保留3位小数的几种方法

import java.math.BigDecimal;

import java.text.DecimalFormat;

import java.text.NumberFormat;

public class format {

double f = 0.5585;

double f1 = 11.5585;

public void m1() {

//数字

BigDecimal bg = new BigDecimal(f);

double f1 = bg.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();

System.out.println(f1);

}

/**

* DecimalFormat转换最简便

*/

public void m2() {

//字符串

DecimalFormat df = new DecimalFormat(“0.000″);//对于大于1的用”#.000″,小于1的要用”0.000”

String t=df.format(f);

System.out.println(t);

DecimalFormat df1 = new DecimalFormat(“#.000”);

System.out.println(df1.format(f1));

}

/**

* String.format打印最简便

*/

public void m3() {

//字符串

String t =String.format(“%.3f”, f);

System.out.println(t);

}

public void m4() {

//字符串

NumberFormat nf = NumberFormat.getNumberInstance();

nf.setMaximumFractionDigits(3);

String t =nf.format(f);

System.out.println(t);

}

public static void main(String[] args) {

format f = new format();

f.m1();

f.m2();

f.m3();

f.m4();

}

}

结果:

0.558

0.558

11.558

0.559

0.558

Math.ceilMath.roundMath.floor

floor 向下取整 ceil  向上取整 round 则是4舍5入的计算,round方法,它表示“四舍五入”,算法为Math.floor(x+0.5),即将原来的数字加上0.5后再向下取整,所以,Math.round(11.5)的结果为12,Math.round(-11.5)的结果为-11。 Math.floor(1.4)=1.0 Math.round(1.4)=1 Math.ceil(1.4)=2.0 Math.floor(1.5)=1.0 Math.round(1.5)=2 Math.ceil(1.5)=2.0 Math.floor(1.6)=1.0 Math.round(1.6)=2 Math.ceil(1.6)=2.0 Math.floor(-1.4)=-2.0 Math.round(-1.4)=-1 Math.ceil(-1.4)=-1.0 Math.floor(-1.5)=-2.0 Math.round(-1.5)=-1 Math.ceil(-1.5)=-1.0 Math.floor(-1.6)=-2.0 Math.round(-1.6)=-2 Math.ceil(-1.6)=-1.0

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/135139.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月5日 下午6:36
下一篇 2022年6月5日 下午6:36


相关推荐

  • 毕业论文管理系统_本科毕业论文网

    毕业论文管理系统_本科毕业论文网毕业论文管理系统1.首先是个简约的登录页面,登录页面分为三个角色,分别是学生,老师和管理员系统的登录界面2.接下来是用户模块,分别为两个模块教师和学生的,管理员可以进行教师和学生添加和修改。3.系部管理4.文件管理,学生上传的论文,管理员和教师都可以进行下载和查看5.学生模块,学生选题,教师发布的题目学生可以进行选择。学生也可以自己申请题目6.学生申请课题7.论文初稿,学生上传论文初稿,等指导老师通过后才能进行最后的终稿8.学生上传论文,指导老师可以提建议,学生可以查看指导老师的建议9.

    2025年6月28日
    9
  • MVC学习笔记八:WebGrid控件的高级使用「建议收藏」

    MVC学习笔记八:WebGrid控件的高级使用「建议收藏」WebGrid控件的高级使用在笔记三中记录了WebGrid的简单使用,但实际工作中并不能满足开发要求,比如:考虑到性能,要求服务器端分页,而不是查出所有数据来进行简单的客户端页面分页;要在排序时,给列标题显示不同图像等等,都不是直接就能满足的,这里记录下对WebGrid进行的较高层次的使用。一.服务器端分页处理在演示服务端分页之前,先做一些简单的准备工作:

    2022年10月6日
    5
  • TinyProxy电信

    TinyProxy电信listen_port=65080;daemon=on;worker_proc=0;uid=3004;http_ip=112.80.255.21;http_port=443;http_del=“X-Online-Host,Host”;http_first=”[M]http://[H][U][V]\r\nHost:[H]\r\nX-T5-Auth:ZjQxNDIh\r\n”;https_connect=on;https_ip=112.80.255.21;https_port=443

    2022年7月27日
    21
  • SQL查询:并集、差集、交集

    SQL查询:并集、差集、交集新建两个表进行测试 test aIDname1 曹操 2 郭嘉 3 孙权 4 周瑜 test bIDname1 刘备 2 关羽 3 张飞 4 孙权 5 周瑜 1 UNION 形成并集 UNION 可以对两个或多个结

    2026年3月16日
    2
  • 初识华为MDC智能驾驶计算平台

    初识华为MDC智能驾驶计算平台初识华为 MDC 智能驾驶计算平台世界智能大会下的世界智能驾驶挑战赛组织方推荐 华为 MDC 是面向自动驾驶领域的计算平台 MDC 拥有自研 CPU 和 AI 芯片 联合车规级 AUTOSAR 平台 促进自动驾驶领域的快速发展 Autoware 的代码看得博主头疼 换换口味 这篇博客不讲技术工程实现细节 谈些宏观些的概念 其实抬头看看也是很重要的 一直埋头干技术细节 跑偏了最后也是失败 博主解读 Autoware 的

    2026年3月18日
    2
  • greater和less的用法

    greater和less的用法greater 使内置类型从大到小排序 而 less 从小到大注意 sort 用 greater 排序 则 a 0 到 a n 有大到小排序 priority queue 用 greater 排序 则先取出的是最小值 includeiostr includealgor intmain inta 5

    2026年3月18日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号