java中常用数据类型转换器
2024-10-19 00:24:12
/**
* 把String转换成long
*
* @param src 要转换的String
* @param def 转换失败时返回此值
* @return 转换好的long
*/
public static long toLong(String src, long def) {
if (src == null) {
return def;
}
try {
return Long.parseLong(src);
} catch (Exception ignore) {
}
return def;
}
/**
* 把String转换成int
*
* @param src 要转换的String
* @param def 转换失败时返回此值
* @return 转换好的int
*/
public static int toInt(String src, int def) {
if (src == null) {
return def;
}
try {
return Integer.parseInt(src);
} catch (Exception ignore) {
}
return def;
} /**
* 把String转换成byte
*
* @param src 要转换的String
* @param def 转换失败时返回此值
* @return 转换好的intbyte
*/
public static byte toByte(String src, byte def) {
if (src == null) {
return def;
}
try {
return Byte.parseByte(src);
} catch (Exception ignore) {
}
return def;
}
/**
* String转换成Date
*
* @param date String
* @param pattern 样式
* @return Date
*/
public static Date toDate(String date, String pattern) {
if (date == null || pattern == null) {
return null;
}
try {
SimpleDateFormat format = new SimpleDateFormat(pattern);
return format.parse(date);
} catch (Exception ignore) {
}
return null;
}
/**
* 分转元转换,保留两位小数
* @param srcValue 源数据
* @return 结果
*/
public static double longToMoney(long srcValue) {
BigDecimal obj = new BigDecimal(srcValue);
double cny = obj.divide(new BigDecimal(100)).doubleValue();
return Double.parseDouble(String.format("%.2f", cny));
}
最新文章
- jquery选项卡
- 微信小程序--摸索之旅
- 《ASP.NET MVC4 WEB编程》学习笔记------HtmlHelper
- PHP操作Memcache基本函数的方法
- Java求和
- c++制作小游戏--雷电
- [转]hive中order by,distribute by,sort by,cluster by
- python django 自定义 装饰器
- TFS跨版本Merge测试
- phpMyAdmin 手动输入数据库服务器IP
- WPF学习(1)WPF概述
- Ruby 一些经常使用的细节
- sql必知必会-总结篇
- [转载]历上最强的音乐播放器(jetAudio-8.0.5.320-Plus-VX
- 本机不安装Oracle客户端,使用PL/SQL Developer和 Instant Client 工具包连接oracle 11g远程数据库
- Redis数据持久化、数据备份、数据的故障恢复
- js 面向对象 ES5 AND ES6
- AtCoder Beginner Contest 049 &; ARC065 連結 / Connectivity AtCoder - 2159 (并查集)
- IntelliJ IDEA使用教程(简介)
- unable to auto-detect email address