Skip to content

Commit

Permalink
#134 手工合并PR#134部分内容,thank you @zhanggh
Browse files Browse the repository at this point in the history
  • Loading branch information
calvin1978 committed Nov 9, 2018
1 parent 419418f commit 0336488
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 8 deletions.
31 changes: 23 additions & 8 deletions vjkit/src/main/java/com/vip/vjtools/vjkit/io/IOUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vip.vjtools.vjkit.io;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -23,20 +24,20 @@
*
* 建议使用Apache Commons IO和Guava关于IO的工具类(com.google.common.io.*), 在未引入Commons IO时可以用本类做最基本的事情.
*
* <p>
* 1. 安静关闭Closeable对象
* <p>
*
* 2. 读出InputStream/Reader全部内容到String 或 List<String>
* <p>
* 3. 读出InputStream一行内容到String
* <p>
*
* 3. 读出InputStream/Reader一行内容到String
*
* 4. 将String写到OutputStream/Writer
* <p>
*
* 5. 将String 转换为InputStream/Reader
*
* 5. InputStream/Reader与OutputStream/Writer之间复制的copy
*
*/
public class IOUtil {
private static final String CLOSE_ERROR_MESSAGE = "IOException thrown while closing Closeable.";

private static Logger logger = LoggerFactory.getLogger(IOUtil.class);

Expand All @@ -52,7 +53,7 @@ public static void closeQuietly(Closeable closeable) {
try {
closeable.close();
} catch (IOException e) {
logger.warn(CLOSE_ERROR_MESSAGE, e);
logger.warn("IOException thrown while closing Closeable.", e);
}
}

Expand Down Expand Up @@ -121,6 +122,20 @@ public static void write(final String data, final Writer output) throws IOExcept
}
}

/**
* 字符串转换成InputStream
*/
public static InputStream toInputStream(String input) {
return new ByteArrayInputStream(input.getBytes(Charsets.UTF_8));
}

/**
* 字符串转换成Reader
*/
public static Reader toInputStreamReader(String input) {
return new InputStreamReader(toInputStream(input), Charsets.UTF_8);
}

/**
* 在Reader与Writer间复制内容
*
Expand Down
170 changes: 170 additions & 0 deletions vjkit/src/main/java/com/vip/vjtools/vjkit/number/MoneyUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package com.vip.vjtools.vjkit.number;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;

import org.apache.commons.lang3.StringUtils;

/**
* 货币工具类.
*
* 1.元和分之间的转换
* 2.货币格式化成字符串
* 3.字符串转换成货币
*
*/
public class MoneyUtil {

private static final ThreadLocal<DecimalFormat> DEFAULT_FORMAT = createThreadLocalNumberformat("0.00");

private static final ThreadLocal<DecimalFormat> PRETTY_FORMAT = createThreadLocalNumberformat("#,##0.00");

// ThreadLocal重用MessageDigest
private static ThreadLocal<DecimalFormat> createThreadLocalNumberformat(final String pattern) {
return new ThreadLocal<DecimalFormat>() {
@Override
protected DecimalFormat initialValue() {
DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance();
df.applyPattern(pattern);
return df;

}
};
}


/////////////////// 元和分的转换 ///////////////////

/**
* 人民币金额单位转换,分转换成元,取两位小数 例如:150 => 1.5
*/
public static BigDecimal fen2yuan(BigDecimal num) {
return num.divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
}

/**
* 人民币金额单位转换,分转换成元,取两位小数 例如:150 => 1.5
*/
public static BigDecimal fen2yuan(long num) {
return fen2yuan(new BigDecimal(num));
}

/**
* 人民币金额单位转换,分转换成元,取两位小数 例如:150 => 1.5
*/
public static BigDecimal fen2yuan(String num) {
if (StringUtils.isEmpty(num)) {
return new BigDecimal(0);
}
return fen2yuan(new BigDecimal(num));
}

/**
* 人民币金额单位转换,元转换成分,例如:1 => 100
*/
public static BigDecimal yuan2fen(String y) {
return new BigDecimal(Math.round(new BigDecimal(y).multiply(new BigDecimal(100)).doubleValue()));
}

/**
* 人民币金额单位转换,元转换成分,例如:1 => 100
*/
public static BigDecimal yuan2fen(double y) {
return yuan2fen(String.valueOf(y));
}

/**
* 人民币金额单位转换,元转换成分,例如:1 => 100
*/
public static BigDecimal yuan2fen(BigDecimal y) {
if (y != null) {
return yuan2fen(y.toString());
} else {
return new BigDecimal(0);
}
}

////////////////// 格式化输出 //////////////////
/**
* 格式化金额,例如:1=>1.00
*/
public static String format(BigDecimal number) {
return format(number.doubleValue());
}

/**
* 格式化金额,默认格式:00.0 ,例如:1=>1.00
*/
public static String format(double number) {
return DEFAULT_FORMAT.get().format(number);
}

/**
* 格式化金额,默认格式:#,##0.00 ,例如:33999999932.3333d 输出:33,999,999,932.33
*/
public static String prettyFormat(BigDecimal number) {
return prettyFormat(number.doubleValue());
}

/**
* 格式化金额,默认格式:#,##0.00 ,例如:33999999932.3333d 输出:33,999,999,932.33
*/
public static String prettyFormat(double number) {
return PRETTY_FORMAT.get().format(number);
}

/**
* 格式化金额,当pattern为空时,pattern默认为#,##0.00
*/
public static String formatNumber(BigDecimal number, String pattern) {
return formatNumber(number.doubleValue(), pattern);
}

/**
* 格式化金额,当pattern为空时,pattern默认为#,##0.00
*/
public static String formatNumber(double number, String pattern) {
DecimalFormat df = null;
if (StringUtils.isEmpty(pattern)) {
df = PRETTY_FORMAT.get();
} else {
df = (DecimalFormat) DecimalFormat.getInstance();
df.applyPattern(pattern);
}

return df.format(number);
}

/////////////// 转换金额字符串为金额//////////

/**
* 分析格式为0.00格式的字符串
*/
public static BigDecimal parseString(String numberStr) throws ParseException {
return new BigDecimal(DEFAULT_FORMAT.get().parse(numberStr).doubleValue());
}

/**
* 分析格式为#,##0.00格式的字符串
*/
public static BigDecimal parsePrettyString(String numberStr) throws ParseException {
return new BigDecimal(PRETTY_FORMAT.get().parse(numberStr).doubleValue());
}

/**
* 按格式分析字符串,当pattern为空时,pattern默认为#,##0.00
*/
public static BigDecimal parseString(String numberStr, String pattern) throws ParseException {
DecimalFormat df = null;
if (StringUtils.isEmpty(pattern)) {
df = PRETTY_FORMAT.get();
} else {
df = (DecimalFormat) DecimalFormat.getInstance();
df.applyPattern(pattern);
}

return new BigDecimal(df.parse(numberStr).doubleValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.vip.vjtools.vjkit.number;

import static org.assertj.core.api.Assertions.*;

import java.math.BigDecimal;

import org.junit.Test;

public class MoneyUtilTest {

@Test
public void amountConvertTest() {
// 金额分转换成元
assertThat(MoneyUtil.fen2yuan(100).doubleValue()).isEqualTo(new BigDecimal(1.00d).doubleValue());
assertThat(MoneyUtil.fen2yuan("100").doubleValue()).isEqualTo(new BigDecimal(1.00d).doubleValue());
assertThat(MoneyUtil.fen2yuan(BigDecimal.valueOf(100d)).doubleValue())
.isEqualTo(new BigDecimal(1.00d).doubleValue());

// 金额元转换成分
assertThat(MoneyUtil.yuan2fen(BigDecimal.valueOf(1d)).doubleValue())
.isEqualTo(new BigDecimal(100d).doubleValue());
assertThat(MoneyUtil.yuan2fen(1L).doubleValue()).isEqualTo(new BigDecimal(100d).doubleValue());
}
}

0 comments on commit 0336488

Please sign in to comment.