「java在线格式化」java自动格式化
今天给各位分享java在线格式化的知识,其中也会对java自动格式化进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java数字如何格式化?
public static String xxx(String aa) {
String flag = "";
if (aa.length() 4) {
flag = aa.substring(0, 3) + "." + aa.substring(3, 4);
} else {
flag = aa;
}
return flag;
}
//当然可以继续判断大于5为四舍五入
延展阅读:
Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。
JAVA里面如何格式化数字
楼主你好!给你写了个测试类希望能帮助你。这两个个方法只需要传入你要格式话的数据,就可以返回你想要的结果了。 package com.line;public class T9 {
/**
* b格式化一般数据为财务格式,eg:123,456,789.00/b
*
* @param source
* String
* @return String
*/
public static String getCaiWuData(String source) {
StringBuffer str = new StringBuffer("");
if (source != null !source.equals("") source.length() 0
!source.equals("null")) {
if (source.lastIndexOf(",") 0) {
source =formatStr(source);
}
int dotIndex = 0;
if (source.indexOf(".") 0) {
source += ".00";
}
dotIndex = source.indexOf(".");
int index = 0;
String opt = "";
opt = source.substring(0, 1);
if (opt.equals("-")) {
source = source.substring(1);
str.append("-");
dotIndex = source.indexOf(".");
}
if (dotIndex 3) {
index += 1;
str.append(source.substring(0, dotIndex));
}
if (dotIndex % 3 == 0) {
index += dotIndex / 3;
} else {
index += (dotIndex - dotIndex % 3) / 3;
}
if (index 0 dotIndex = 3) {
for (int i = index; i 0; i--) {
if (i == index) {
str.append(source.substring(0, dotIndex - i * 3));
}
if (dotIndex - i * 3 0) {
str.append(",");
}
if (i = 1) {
str.append(source.substring(dotIndex - i * 3, dotIndex
- (i - 1) * 3));
}
}
}
str.append(source.substring(dotIndex));
}
if (source.length() - source.lastIndexOf(".") 3) {
str.append("0");
}
int dot_index = str.toString().indexOf(".") + 2;
int str_len = str.toString().length();
char[] strArr = str.toString().toCharArray();
StringBuffer rev = new StringBuffer();
for (int i = str_len - 1; i 0; i--) {// 除去尾数0,小数点后保留2位
if (i dot_index
Integer.parseInt(new Character(strArr[i]).toString()) 0) {
rev.append(str.toString().substring(0, i + 1));
break;
} else if (i == dot_index (int) strArr[i] = 0) {
rev.append(str.toString().substring(0, dot_index + 1));
break;
}
}
return rev.toString();
}
/**
* b格式化财务数据为一般字符串,eg:123456789.00/b
*
* @param source
* String
* @return String
*/
public static String formatStr(String source) {
StringBuffer str = new StringBuffer("");
if (source != null !source.equals("") source.length() 0
!source.equals("null")) {
String temp = source.substring(0, 1);
if (temp.equals("-")) {
source = source.substring(1);
str.append("-");
}
String[] myarr = source.split(",");
int lastIndex = source.lastIndexOf(",");
if (lastIndex 0) {
for (int i = 0; i myarr.length; i++) {
str.append(myarr[i]);
}
}
if (source.lastIndexOf(",") 0) {
str.append(source);
}
if (source.lastIndexOf(".") 0) {
str.append(".00");
}
if (source.length() - source.lastIndexOf(".") 3
!"0".equals(source)) {
str.append("0");
}
} else {
return (str.append("0.00").toString());
}
return str.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
T9 t=new T9();
System.out.println(t.getCaiWuData("1231313"));
System.out.println(t.formatStr("1,231,313.00"));
}}
java 怎样将string 格式化
在java中,将浮点数格式化为string,一般使用DecimalFormat。DecimalFormat的用法示例如下:DecimalFormatdf=newDecimalFormat();doubledata=1234.56789;System.out.println("格式化之前的数字:"+data);Stringstyle="0.0";//定义要显示的数字的格式df.applyPattern(style);//将格式应用于格式化器System.out.println("采用style:"+style+"格式化之后:"+df.format(data));style="00000.000kg";//在格式后添加诸如单位等字符df.applyPattern(style);System.out.println("采用style:"+style+"格式化之后:"+df.format(data));//模式中的"#"表示如果该位存在字符,则显示字符,如果不存在,则不显示。style="##000.000kg";df.applyPattern(style);System.out.println("采用style:"+style+"格式化之后:"+df.format(data));//模式中的"-"表示输出为负数,要放在最前面style="-000.000";df.applyPattern(style);System.out.println("采用style:"+style+"格式化之后:"+df.format(data));//模式中的","在数字中添加逗号,方便读数字style="-0,000.0#";df.applyPattern(style);System.out.println("采用style:"+style+"格式化之后:"+df.format(data));//模式中的"E"表示输出为指数,"E"之前的字符串是底数的格式,//"E"之后的是字符串是指数的格式style="0.00E000";df.applyPattern(style);System.out.println("采用style:"+style+"格式化之后:"+df.format(data));//模式中的"%"表示乘以100并显示为百分数,要放在最后。style="0.00%";df.applyPattern(style);System.out.println("采用style:"+style+"格式化之后:"+df.format(data));//模式中的"\u2030"表示乘以1000并显示为千分数,要放在最后。style="0.00\u2030";//在构造函数中设置数字格式DecimalFormatdf1=newDecimalFormat(style);//df.applyPattern(style);System.out.println("采用style:"+style+"格式化之后:"+df1.format(data));下面是总结:格式化之前的数字:1234.56789采用style:0.0格式化之后:1234.6采用style:00000.000kg格式化之后:01234.568kg采用style:##000.000kg格式化之后:1234.568kg采用style:-000.000格式化之后:-1234.568采用style:-0,000.0#格式化之后:-1,234.57采用style:0.00E000格式化之后:1.23E003采用style:0.00%格式化之后:123456.79%采用style:0.00‰格式化之后:1234567.89‰
JAVA 中获取时间怎么格式化?
时间格式化输出主要有两种方式,代码如下:
//使用Calendar
Calendar now = Calendar.getInstance();
System.out.println("年:" + now.get(Calendar.YEAR));
System.out.println("月:" + (now.get(Calendar.MONTH) + 1));
System.out.println("日:" + now.get(Calendar.DAY_OF_MONTH));
System.out.println("时:" + now.get(Calendar.HOUR_OF_DAY));
System.out.println("分:" + now.get(Calendar.MINUTE));
ystem.out.println("秒:" + now.get(Calendar.SECOND));
//使用Date
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("当前时间:" + sdf.format(d));
扩展资料
JAVA中获取当前系统时间。
import java.util.Date;
import java.text.SimpleDateFormat;
public class NowString {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
}
}
参考资料来源:百度百科:Java
关于java在线格式化和java自动格式化的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-23,除非注明,否则均为
原创文章,转载请注明出处。