首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

java取整和java四舍五入方法

double i=2, j=2.1, k=2.5, m=2.9; System.out.println(“舍掉小数取整:Math.floor(2)=” + (int)Math.floor(i)); System.out.println(“舍掉小数取整:Math.floor(2.1)=” + (int)Math.floor(j)); System.out.println(“舍掉小数取整:Math.floor(2.5)=” + (int)Math.floor(k)); System.out.println(“舍掉小数取整:Math.floor(2.9)=” + (int)Math.floor(m)); /* 这段被注释的代码不能正确的实现四舍五入取整 System.out.println(“四舍五入取整:Math.rint(2)=” + (int)Math.rint(i)); System.out.println(“四舍五入取整:Math.rint(2.1)=” + (int)Math.rint(j)); System.out.println(“四舍五入取整:Math.rint(2.5)=” + (int)Math.rint(k)); System.out.println(“四舍五入取整:Math.rint(2.9)=” + (int)Math.rint(m)); System.out.println(“四舍五入取整:(2)=” + new DecimalFormat(“0”).format(i)); System.out.println(“四舍五入取整:(2.1)=” + new DecimalFormat(“0”).format(i)); System.out.println(“四舍五入取整:(2.5)=” + new DecimalFormat(“0”).format(i)); System.out.println(“四舍五入取整:(2.9)=” + new DecimalFormat(“0”).format(i)); */ System.out.println(“四舍五入取整:(2)=” + new BigDecimal(“2”).setScale(0, BigDecimal.ROUND_HALF_UP)); System.out.println(“四舍五入取整:(2.1)=” + new BigDecimal(“2.1”).setScale(0, BigDecimal.ROUND_HALF_UP)); System.out.println(“四舍五入取整:(2.5)=” + new BigDecimal(“2.5”).setScale(0, BigDecimal.ROUND_HALF_UP)); System.out.println(“四舍五入取整:(2.9)=” + new BigDecimal(“2.9”).setScale(0, BigDecimal.ROUND_HALF_UP));

01
您找到你想要的搜索结果了吗?
是的
没有找到

MySQL数据库:第五章:常见函数

函数:类似于java中的”方法“,将完成某个功能的一系列步骤封装起来,对外暴露一个名字,供外界调用 当我们学习别人定义好的方法(函数),只需要关心两件事: ①叫什么(函数名) ②干什么(函数功能) sql中的函数:肯定有,并且仅有一个返回值 调用语法: select 函数名(实参列表); 一、数学函数: abs绝对值 mod取余 floor向下取整 truncate 截断 ceil向上取整 round四舍五入 注意: sql中的round支持两个重载,规则:先按绝对值四舍五入,然后再添加正负 round(x):只保留整数部位 round(x,d):保留小数点后一位 java中Math.round只支持一个参数,规则:Math.round(x)等价于Math.floor(x+0.5) Math.round(x) rand随机数 二、字符函数 upper转换成大写 lower转换成小写 length获取字节长度 char_length获取字符长度 substr截取子串 trim去掉前后空格或字符 concat拼接 strcmp比较两个字符串 instr获取子串第一次出现的索引,如果找不到,返回0 注意:sql中起始索引,一般从1开始! 三、日期函数 now当前日期+时间 curdate当前日期 curtime当前时间 datediff两个日期天数差 date_format日期——>字符 str_to_date字符——>日期 四、流程控制函数 1、if函数 2、case结构 形式1:类似于switch 形式2:类似于多重if

02
领券