关于java的学习,通常需要我们能够全身心地投入到其中,全神贯注地学习才能有所收获。今天来为大家介绍一下,java数字处理类的一些内容,java中数字格式化和math类的内容,并且通过详细的图片解析。
首先,我们需要了解的是,java主要对浮点类型数据(double型和float型)进行数字格式化操作。DecimalFormat是NumberFormat的一个子类,用于格式化十进制数字。需要代码如下所示:import java.text.DecimalFormat;
然后是设置格式化模式的方法如下:1. DecimalFormat myFormat = new DecimalFormat(pattern);
2. DecimalFormat myFormat = new DecimalFormat();
myFormat.applyPattern(pattern);
3. DecimalFormat myFormat1 = new DecimalFormat();
myFormat1.setGroupingSize(2);
4. DecimalFormat myFormat3 = new DecimalFormat();
myFormat3.setGroupingUsed(false);
代码展示如下:import java.text.DecimalFormat;
public class decimalFormatSimpleDemo
{
static public void SimgleFormat(String pattern, double value)
{
//使用实例化对象时设置格式化模式,实例化DecialFormat对象
DecimalFormat myFormat = new DecimalFormat(pattern);
String output = myFormat.format(value);
System.out.println(value + " " + pattern + " " + output);
}
static public void UseApplyPatternMethodFormat(String pattern, double value)
{
//使用applyyPattern()方法对数字进行格式化
DecimalFormat myFormat = new DecimalFormat();
myFormat.applyPattern(pattern);
String output = myFormat.format(value);
System.out.println(value + " " + pattern + " " + output);
}
static public void SpecialTypeMethods(double value)
{
DecimalFormat myFormat1 = new DecimalFormat();
myFormat1.setGroupingSize(2); //特殊方法一
System.out.println(value +
" myFormat1.setGroupingSize(2) " + myFormat1.format(value));
DecimalFormat myFormat2 = new DecimalFormat();
myFormat2.setGroupingUsed(false); //特殊方法二
System.out.println(value +
" myFormat2.setGroupingUsed(true) " + myFormat2.format(value));
DecimalFormat myFormat3 = new DecimalFormat();
myFormat3.setGroupingUsed(true);
System.out.println(value +
" myFormat3.setGroupingUsed(false) " + myFormat3.format(value));
}
public static void main(String[] args)
{
String pattern1 = "###,###.###";
Double d = 123456.789;
SimgleFormat(pattern1, d);
String pattern2 = "#########.###%";
UseApplyPatternMethodFormat(pattern2, d);
SpecialTypeMethods(d);
}
}
运行结果如下图:
接下来为大家介绍math类的内容:
⑴三角函数方法,图片展示如下:
⑵指数函数方法,图片展示如下:
⑶取整函数方法,图片展示如下:
⑷取最大值、最小值、绝对值函数方法,图片展示如下:
以上就是关于java中数字格式化和math类的主要内容,并且通过详细的图片为大家解析。如果你对java知识感兴趣,想要了解更多java基础,敬请关注奇Q工具网。
推荐阅读: