xLang 的类型转换
今天学习了xLang 的类型转换
1、如果想要把 Int 转换为 String,可以使用 String.format("%d", 123)
例如
int a = 2;
string b = "";
b = String.format("%d", a);
这里的 %d 部分,可以替换成其他(列举常用的):
%d 整数
%f 浮点数,默认保留6位小数点
%s 字符串
%c 字符
%.[n]f 是保留n位小数点
%e 是格式化浮点数为科学计数法
%[n]d 格式化整数,n是保留几位整数,例如 format("%02d", 1) 就是 "01"
%g 是格式化浮点数
%s 是格式化字符串
%X是格式化为十六进制数
%p 是格式化指针 用的很少
String.formatDate 用于格式化日期
%d 和 %[n]d
%c 是格式化字符
%f 是格式化浮点数,默认保留6位小数点
%e 是格式化浮点数为科学计数法
%p 是格式化指针 用的很少
String.formatDate 格式化代码
/**
*@brief 格式化日期字符串
*@param fmt 格式 %Y年 %m月 %d日 %H时 %M分 %S 秒 可用格式如下
* a: abbreviated weekday name
* A: full name of the weekday
* b: abbreviated month name
* B: full name of month
* c: locale's date and time reprensentation
* d: day of the month (01-31)
* H: hour of the day (00-23)
* I: hour of the day (01-12)
* j: day of the year (001-366)
* m: month of the year (01-12)
* M: minute (00-59)
* p: locale's version of AM or PM
* S: seconds (00-59)
* U: week number of the year (00-53) with Sunday as the first day of the week
* w: weekday (Sunday = 0 to Saturday = 6)
* W: week number of the year (00-53) with Monday as the first day of the week
* x: locale's date representation
* X: locale's time representation
* y: two-digit year representation (00-99)
* Y: four-digit year representation
* Z: time zone name
*@millis 毫秒数
*@return 格式化结果
*/
static String formatDate(String fmt, long millis);
2、如果想把 String 转换成 Int ,可以使用 parseInt()
例如
string a = "1234";
string b = "7890";
string c = a + b;
int d = c.parseInt();