本帖最后由 寇龙飞 于 2012-9-15 18:22 编辑
- package com.itheima.test;
- public class KeyTest2 {
- public static void main(String[] args) {
- double data = 3.1415926;
- data = setDoubleFormat(data, 1);
- System.out.println(data);
- data = setDoubleFormat(data, 2);
- System.out.println(data);
- data = setDoubleFormat(data, 3);
- System.out.println(data);
- data = setDoubleFormat(data, 4);
- System.out.println(data);
- }
- /*想要实现的功能:返回val小数点后newScale位的数字*/
- public static double setDoubleFormat(double val, int newScale) {
-
- return (int)(val * Math.pow(10, newScale)) / Math.pow(10, newScale);
- }
- }
复制代码 咋打印结果全是3.1啊? |