Skip to content

Commit 1a490d9

Browse files
committed
修复bug
1 parent 55d918a commit 1a490d9

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

src/main/java/cn/fudoc/trade/util/NumberFormatUtil.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,30 @@
44
import org.apache.commons.lang3.StringUtils;
55

66
import java.math.BigDecimal;
7+
import java.math.RoundingMode;
78
import java.text.DecimalFormat;
89
import java.util.Objects;
910

1011
public class NumberFormatUtil {
1112

13+
private static final BigDecimal ONE_HUNDRED = new BigDecimal(100);
1214

1315
public static String format(BigDecimal value) {
1416
DecimalFormat decimalFormat = new DecimalFormat(",###.##");
1517
return decimalFormat.format(value);
1618
}
1719

18-
public static String formatRate(BigDecimal value) {
20+
public static String formatRate(BigDecimal value, boolean flag) {
1921
DecimalFormat decimalFormat = new DecimalFormat("#.##%");
22+
if (flag) {
23+
value = value.divide(ONE_HUNDRED, 5, RoundingMode.CEILING);
24+
}
2025
return decimalFormat.format(value);
2126
}
2227

2328

2429
public static BigDecimal convertBigDecimal(Object value) {
25-
if(Objects.isNull(value)){
30+
if (Objects.isNull(value)) {
2631
return BigDecimal.ZERO;
2732
}
2833
String strValue = value.toString().trim();

src/main/java/cn/fudoc/trade/view/render/StockColorTableCellRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public StockColorTableCellRenderer(Integer left) {
2222
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
2323
if (Objects.nonNull(value)) {
2424
BigDecimal bigDecimal = NumberFormatUtil.convertBigDecimal(value);
25-
value = NumberFormatUtil.formatRate(bigDecimal);
25+
value = NumberFormatUtil.formatRate(bigDecimal,true);
2626
JBColor textColor = getTextColor(bigDecimal);
2727
if (Objects.nonNull(textColor)) {
2828
setForeground(textColor);

src/main/java/cn/fudoc/trade/view/table/HoldStockGroupTableView.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public HoldStockGroupTableView(String tabName) {
4545
int rowHeight = stockTable.getRowHeight();
4646
stockTable.setRowHeight(rowHeight * 2);
4747
for (String columnName : getColumnNames()) {
48-
stockTable.getColumn(columnName).setCellRenderer(new MultiLineTableCellRenderer(Lists.newArrayList(1, 4), Lists.newArrayList( 1, 3, 4)));
48+
stockTable.getColumn(columnName).setCellRenderer(new MultiLineTableCellRenderer(Lists.newArrayList(1, 4), Lists.newArrayList(1, 3, 4)));
4949
}
5050
TableColumn idColumn = stockTable.getColumnModel().getColumn(0);
5151
// 从视图中移除,模型仍保留
@@ -149,7 +149,7 @@ protected Vector<Object> toTableData(RealStockInfo realStockInfo) {
149149

150150
//今日收益计算 (当前价-上一日收盘价)*持仓数量
151151
BigDecimal yesterdayPrice = new BigDecimal(realStockInfo.getYesterdayPrice());
152-
BigDecimal increaseRate = new BigDecimal(realStockInfo.getIncreaseRate()).divide(new BigDecimal("100"), 5, RoundingMode.CEILING);
152+
BigDecimal increaseRate = new BigDecimal(realStockInfo.getIncreaseRate());
153153
BigDecimal todayProfit = currentPrice.subtract(yesterdayPrice).multiply(countDecimal).setScale(4, RoundingMode.CEILING);
154154

155155
//表格数据
@@ -161,15 +161,15 @@ protected Vector<Object> toTableData(RealStockInfo realStockInfo) {
161161
//持仓盈亏
162162
String PLRatePrefix = PLRate.compareTo(BigDecimal.ZERO) > 0 ? "+" : "";
163163
String PLPrefix = PL.compareTo(BigDecimal.ZERO) > 0 ? "+" : "";
164-
vector.add(new String[]{PLPrefix + NumberFormatUtil.format(PL), PLRatePrefix + NumberFormatUtil.formatRate(PLRate)});
164+
vector.add(new String[]{PLPrefix + NumberFormatUtil.format(PL), PLRatePrefix + NumberFormatUtil.formatRate(PLRate, false)});
165165
//持仓数量
166166
vector.add(count);
167167
//现价/成本
168168
vector.add(new String[]{realStockInfo.getCurrentPrice(), cost.setScale(3, RoundingMode.CEILING).toString()});
169169
//今日收益
170170
String todayProfitPrefix = todayProfit.compareTo(BigDecimal.ZERO) > 0 ? "+" : "";
171171
String profitRatePrefix = increaseRate.compareTo(BigDecimal.ZERO) > 0 ? "+" : "";
172-
vector.add(new String[]{todayProfitPrefix + NumberFormatUtil.format(todayProfit), profitRatePrefix + NumberFormatUtil.formatRate(increaseRate)});
172+
vector.add(new String[]{todayProfitPrefix + NumberFormatUtil.format(todayProfit), profitRatePrefix + NumberFormatUtil.formatRate(increaseRate, true)});
173173
return vector;
174174
}
175175

0 commit comments

Comments
 (0)