资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

格式化java代码 Java数据格式化

java数字格式化

用 %3.2f格式化后, 替换逗号即可;

我们提供的服务有:成都网站制作、网站建设、微信公众号开发、网站优化、网站认证、船山ssl等。为1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的船山网站制作公司

同理, 用 #,##0.00 格式化后, 替换, 为空格即可.

你的需求:

double x = 1234.5;

DecimalFormat df = new DecimalFormat("#,###.0");

String xs = df.format(x);

xs = xs.replace(",", " ").replace(".", ",");

System.out.println(xs);

输出

1 234,5

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

eclipse怎样对java代码自动排版,快速格式化,快速使代码对齐

使代码快速对齐的方法有两种,咱们先看第一种:首先打开代码

如图所示,找到"Source",点击

在弹出的下拉框内,找到"Format",然后点击

然后对比一下,就可以看到代码自动对齐了

还有一种方法是直接使用快捷键“Ctrl+shift+f”,就ok了

返回代码区,然后对比一下,就可以看到代码自动对齐了

java题,如何将数组中的数据格式化输出?(有代码)

import java.util.*;

class gongzi {

public static void main(String[] args) {

Scanner kb = new Scanner(System. in );

int n = kb.nextInt();

String name = "";

double d = 0;

double e = 0;

double g = 0;

double k = 0;

for (int i = 0; i  n; i++) {

name = kb.next();

d = kb.nextDouble();

e = kb.nextDouble();

g = kb.nextDouble();

k = e + d - g;

}

kb.close();

System.out.printf("%s,Salary is: %.2f + %.2f - %.2f = %.2f%n", name, d, e, g, k);

}

}

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代码 Java数据格式化
文章地址:http://cdkjz.cn/article/docosjc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220