当前位置: 首页 > news >正文

java格式化BigDecimal為#,###,##0.00

Java BigDecimal 格式化工具

我将为您创建一个Java BigDecimal格式化工具,实现将数字格式化为"#,###,##0.00"格式。这个界面允许用户输入数字并查看格式化结果。

设计思路

  • 创建简洁直观的输入界面

  • 实现实时格式化功能

  • 展示格式化前后的对比

  • 添加格式化规则说明

完整实现代码

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Java BigDecimal 格式化工具</title><style>* {margin: 0;padding: 0;box-sizing: border-box;font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;}body {background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);min-height: 100vh;display: flex;justify-content: center;align-items: center;padding: 20px;}.container {max-width: 800px;width: 100%;background: white;border-radius: 12px;box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);overflow: hidden;}.header {background: linear-gradient(135deg, #3498db 0%, #2c3e50 100%);color: white;padding: 30px;text-align: center;}.header h1 {font-size: 28px;margin-bottom: 10px;}.header p {opacity: 0.9;font-size: 16px;}.content {padding: 30px;}.input-section {margin-bottom: 30px;}.input-group {display: flex;margin-bottom: 20px;}input {flex: 1;padding: 12px 15px;border: 2px solid #e0e0e0;border-radius: 8px 0 0 8px;font-size: 16px;outline: none;transition: border-color 0.3s;}input:focus {border-color: #3498db;}button {background: #3498db;color: white;border: none;padding: 0 20px;border-radius: 0 8px 8px 0;cursor: pointer;font-size: 16px;transition: background 0.3s;}button:hover {background: #2980b9;}.result-section {background: #f8f9fa;padding: 20px;border-radius: 8px;margin-bottom: 30px;}.result-title {font-size: 18px;font-weight: 600;margin-bottom: 10px;color: #2c3e50;}.formatted-result {font-size: 24px;font-weight: 700;color: #27ae60;padding: 15px;background: white;border-radius: 8px;text-align: center;border: 2px dashed #e0e0e0;}.examples-section {margin-bottom: 30px;}.examples-title {font-size: 18px;font-weight: 600;margin-bottom: 15px;color: #2c3e50;}.examples-grid {display: grid;grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));gap: 15px;}.example-item {background: #f8f9fa;padding: 15px;border-radius: 8px;border-left: 4px solid #3498db;}.example-original {font-size: 14px;color: #7f8c8d;margin-bottom: 5px;}.example-formatted {font-size: 16px;font-weight: 600;color: #2c3e50;}.info-section {background: #e8f4fc;padding: 20px;border-radius: 8px;}.info-title {font-size: 18px;font-weight: 600;margin-bottom: 15px;color: #2c3e50;}.info-content {line-height: 1.6;color: #34495e;}.info-content ul {padding-left: 20px;margin: 10px 0;}.info-content li {margin-bottom: 8px;}.code-block {background: #2c3e50;color: #ecf0f1;padding: 15px;border-radius: 6px;font-family: 'Courier New', monospace;margin: 15px 0;overflow-x: auto;}@media (max-width: 600px) {.input-group {flex-direction: column;}input {border-radius: 8px;margin-bottom: 10px;}button {border-radius: 8px;padding: 12px;}.examples-grid {grid-template-columns: 1fr;}}</style>
</head>
<body><div class="container"><div class="header"><h1>Java BigDecimal 格式化工具</h1><p>将数字格式化为 #,###,##0.00 格式</p></div><div class="content"><div class="input-section"><div class="input-group"><input type="text" id="numberInput" placeholder="输入数字(例如:1234567.89)"><button id="formatBtn">格式化</button></div></div><div class="result-section"><div class="result-title">格式化结果:</div><div class="formatted-result" id="formattedResult">-,---,--0.00</div></div><div class="examples-section"><div class="examples-title">示例:</div><div class="examples-grid" id="examplesGrid"><!-- 示例将通过JavaScript动态生成 --></div></div><div class="info-section"><div class="info-title">Java 代码实现</div><div class="info-content"><p>在Java中,可以使用以下代码将BigDecimal格式化为<code>#,###,##0.00</code>格式:</p><div class="code-block">
import java.math.BigDecimal;
import java.text.DecimalFormat;public class BigDecimalFormatter {public static String formatBigDecimal(BigDecimal number) {DecimalFormat df = new DecimalFormat("#,###,##0.00");return df.format(number);}public static void main(String[] args) {BigDecimal num = new BigDecimal("1234567.89");String formatted = formatBigDecimal(num);System.out.println(formatted); // 输出: 12,34,567.89}
}</div><p>格式化模式说明:</p><ul><li><code>#</code> - 数字占位符,不显示多余的零</li><li><code>0</code> - 数字占位符,显示多余的零</li><li><code>,</code> - 分组分隔符(千位分隔符)</li><li><code>.##</code> - 小数部分,保留两位小数</li></ul><p>注意:此格式化模式遵循印度数字分组系统,其中数字按千、十万等单位分组。</p></div></div></div></div><script>document.addEventListener('DOMContentLoaded', function() {const numberInput = document.getElementById('numberInput');const formatBtn = document.getElementById('formatBtn');const formattedResult = document.getElementById('formattedResult');const examplesGrid = document.getElementById('examplesGrid');// 示例数据const examples = [{ original: '1234567.89', formatted: '12,34,567.89' },{ original: '987654321.12', formatted: '98,76,54,321.12' },{ original: '1000.5', formatted: '1,000.50' },{ original: '123.456', formatted: '123.46' },{ original: '1234', formatted: '1,234.00' },{ original: '0.123', formatted: '0.12' }];// 生成示例examples.forEach(example => {const exampleItem = document.createElement('div');exampleItem.className = 'example-item';exampleItem.innerHTML = `<div class="example-original">${example.original}</div><div class="example-formatted">${example.formatted}</div>`;examplesGrid.appendChild(exampleItem);});// 格式化函数function formatNumber(value) {// 移除所有非数字字符(除了小数点和负号)const cleanedValue = value.replace(/[^\d.-]/g, '');// 如果是空值或无效值,返回默认格式if (!cleanedValue || isNaN(parseFloat(cleanedValue))) {return '-,---,--0.00';}// 将值转换为数字const num = parseFloat(cleanedValue);// 实现印度数字分组系统const parts = Math.abs(num).toFixed(2).split('.');let integerPart = parts[0];const decimalPart = parts[1];// 处理整数部分的分组let lastThree = integerPart.substring(integerPart.length - 3);const otherNumbers = integerPart.substring(0, integerPart.length - 3);if (otherNumbers !== '') {// 每两位数字添加逗号lastThree = ',' + lastThree;let formattedOther = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",");integerPart = formattedOther + lastThree;}// 添加符号和小数部分let result = (num < 0 ? '-' : '') + integerPart + '.' + decimalPart;return result;}// 格式化按钮点击事件formatBtn.addEventListener('click', function() {const inputValue = numberInput.value.trim();const formatted = formatNumber(inputValue);formattedResult.textContent = formatted;});// 输入框回车事件numberInput.addEventListener('keypress', function(e) {if (e.key === 'Enter') {formatBtn.click();}});// 初始示例formattedResult.textContent = formatNumber('1234567.89');});</script>
</body>
</html>

功能说明

这个工具提供了以下功能:

  1. 数字格式化:将输入的数字格式化为 #,###,##0.00 格式

  2. 实时预览:输入数字后点击"格式化"按钮查看结果

  3. 示例展示:提供多个格式化示例供参考

  4. Java代码:展示如何在Java中实现相同的格式化功能

使用说明

  1. 在输入框中输入要格式化的数字

  2. 点击"格式化"按钮或按Enter键查看结果

  3. 格式化结果将显示在结果区域

  4. 可以参考下方的示例了解不同数字的格式化效果

这个工具模拟了Java中使用DecimalFormat类格式化BigDecimal数字的效果,特别是实现了印度数字分组系统(千、十万等单位分组)。

http://www.dtcms.com/a/592819.html

相关文章:

  • 增城建设网站济南seo排名优化推广
  • 用 Table ID 驯服异构库Flink CDC 跨系统表映射的工程化实践
  • 简洁大气的公司网站外包推广公司
  • MOSFET选型指南:为何ASIM阿赛姆是高效电源设计的优选
  • RV1126 NO.48:RV1126+OPENCV在视频中添加时间戳
  • Transformer实战(25)——自动超参数优化提升Transformer模型性能
  • 得实DS-300针式打印机使用连续纸打印完成后不能自动走到撕纸位置上怎么解决?
  • 大连网站建设在线win7如何做网站服务器
  • 怎样看一个网站做的网络广告郴州网络推广公司
  • 百度智能云 X 十字路口 | 对谈王雁鹏:亲述从大数据时代到 3 万卡集群的中国算力演进史
  • 初识MYSQL —— 索引
  • Blender快捷方式,自用Mark版
  • 移远 5G RG255AA-CN 调试
  • PyTorch3D从CUDA到CPU环境的完整迁移指南
  • 移动通信网络建设-实验2:5G站点选型与设备部署
  • 【自然语言处理】预训练06:子词嵌入
  • 地球的螺旋运动、四季轮回与椭圆轨道:统一场论下的宇宙新图景
  • html格式网站与网站开发有关的岗位是哪些
  • 底层视觉及图像增强-项目实践(十六-0-(6):线性映射技术在LED显示驱动中的工程实践与创新):从奥运大屏,到手机小屏,快来挖一挖里面都有什么
  • 2.7 模型评估与 A/B 测试
  • 政务终端一体化安全解决方案
  • 模板工程的建立
  • 开发者实践:电梯梯控的 非侵入式 与安全模块的电气解耦
  • Redis 高可用集群部署实战:单Docker实现1主2从3
  • 成都在线制作网站作文网入口
  • 想更新公司网站怎么做利于优化的wordpress模板
  • APP开发技术选型:原生 vs 跨端 (Flutter/React Native) 对比与适配场景
  • 智能指针在仓颉技术中的深度实践:从原理到架构的全维解析
  • Flutter开发全攻略:从入门到精通
  • Flutter持续健康发展的多维度分析