包含javaint格式化的词条

博主:adminadmin 2022-12-11 13:00:08 63

今天给各位分享javaint格式化的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

在Java中怎么把Int型的装换成Long型的

有三种方式,

(1)如果你是在做运算,就不用刻意转换,java中会默认向强类型转换。就是int和long在混合运算时,会默认把int转换成long的。

(2)强制类型转化。你在int类型的变量前加:(long);示例:int a= 5; long b = (long) a

(3)先将int类型转换为String类型,在将String类型转换成long类型。示例:int a = 5; String t = String.valueOf(a); long b = Long.parseLong(t);

在java中如何使int 变成日期格式类型

public static void main(String[] args) {

//分配 Date 对象并初始化此对象,以表示自从标准基准时间(称为“历元(epoch)”,

//即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数

Date date =new Date(3600);

//使用日期格式化类完成日期到格式化字符串的输出

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SS");

System.out.println(format.format(date));

}

JAVA中怎么把int型数据转为BigInteger型数据

1、创建java类,TestBigInteger.java;

2、编写java代码;

public class TestBigInteger {

public static void main(String[] args) {

BigInteger bint = new BigInteger(String.valueOf(123));

}

}

3、编写输出结果代码;

System.out.println(bint);

System.out.println(bint.getClass());

4、执行结果,类型为‘class java.math.BigInteger’;

Java中怎么把int型的数字转换成char型的数字

java将int类型的数字转换成char型,主要是通过强制类型转换,如下代码:

public class Int2CharDemo { public static void main(String[] args) {  // 将int类型数字8转换为char类型数字8

  int num1 = 8;

  char ch1 = (char) (num1 + 48);

  System.out.println("ch1 = " + ch1);  // 将char类型数字8转换为int类型数字8

  // 方法一:

  Character ch2 = '8'; // char是基本数据类型,Character是其包装类型。

  int num2 = Integer.parseInt(ch2.toString());

  System.out.println("num2 = " + num2);

  // 方法二:

  char ch3 = '8';

  int num3 = ch3 - 48;

  System.out.println("num3 = " + num3); }

}

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中如何将int i=1;转化成string s="0001" 这用到的什么语法

一般, 使用字符串的格式化方法, 来使数字1.变成字符串"0001"

参考代码如下

public class Test {

public static void main(String[] args) {

int num = 1;

String str = String.format("%04d", num);//字符串格式化. 

//格式化的规则是 0--不足时,前面补充0  4--长度为4  d--正整数

System.out.println(str); // 输出0001

}

}

输出

0001

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

The End

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