「java格式化输入」Java输入格式

博主:adminadmin 2022-12-28 10:30:07 66

本篇文章给大家谈谈java格式化输入,以及Java输入格式对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java数字格式化

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

同理, 用 #,##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的scanner能否格式化输入

import java.util.Scanner;

public class Out {

public static void main(String[] args) {

System.out.println("Please input the information like:abc 35 12 55");

Scanner input = new Scanner (System.in);

String id = input.next();

int i=input.nextInt();

int j=input.nextInt();

int k=input.nextInt();

System.out.println("卡片编号:"+id);

System.out.println("色彩:"+i);

System.out.println("尺寸:"+j);

System.out.println("形状:"+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中如何从格式化字符串输入数值

什么意思??格式化字符串??

是说要按照一定的格式书写么用String.matches("正则表达式")

输入数值可以通过valueof(object

obj);转换成字符串

一般有重写的toString()方法也可以转换成字符串!!

要是这个意思的话,请采纳!!!!

关于java格式化输入和Java输入格式的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-28,除非注明,否则均为首码项目网原创文章,转载请注明出处。