最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java構(gòu)建一個BigDecimal數(shù)字格式化工具

 更新時間:2025年11月11日 09:42:39   作者:悟能不能悟  
這篇文章主要為大家詳細(xì)介紹了如何使用java創(chuàng)建一個BigDecimal格式化工具,實(shí)現(xiàn)將數(shù)字格式化為"#,###,##0.00"格式,希望對大家有所幫助

我將為您創(chuàng)建一個Java BigDecimal格式化工具,實(shí)現(xiàn)將數(shù)字格式化為"#,###,##0.00"格式。這個界面允許用戶輸入數(shù)字并查看格式化結(jié)果。

設(shè)計(jì)思路

創(chuàng)建簡潔直觀的輸入界面

實(shí)現(xiàn)實(shí)時格式化功能

展示格式化前后的對比

添加格式化規(guī)則說明

完整實(shí)現(xiàn)代碼

<!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>將數(shù)字格式化為 #,###,##0.00 格式</p>
        </div>
        
        <div class="content">
            <div class="input-section">
                <div class="input-group">
                    <input type="text" id="numberInput" placeholder="輸入數(shù)字(例如:1234567.89)">
                    <button id="formatBtn">格式化</button>
                </div>
            </div>
            
            <div class="result-section">
                <div class="result-title">格式化結(jié)果:</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動態(tài)生成 -->
                </div>
            </div>
            
            <div class="info-section">
                <div class="info-title">Java 代碼實(shí)現(xiàn)</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> - 數(shù)字占位符,不顯示多余的零</li>
                        <li><code>0</code> - 數(shù)字占位符,顯示多余的零</li>
                        <li><code>,</code> - 分組分隔符(千位分隔符)</li>
                        <li><code>.##</code> - 小數(shù)部分,保留兩位小數(shù)</li>
                    </ul>
                    
                    <p>注意:此格式化模式遵循印度數(shù)字分組系統(tǒng),其中數(shù)字按千、十萬等單位分組。</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');
            
            // 示例數(shù)據(jù)
            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);
            });
            
            // 格式化函數(shù)
            function formatNumber(value) {
                // 移除所有非數(shù)字字符(除了小數(shù)點(diǎn)和負(fù)號)
                const cleanedValue = value.replace(/[^\d.-]/g, '');
                
                // 如果是空值或無效值,返回默認(rèn)格式
                if (!cleanedValue || isNaN(parseFloat(cleanedValue))) {
                    return '-,---,--0.00';
                }
                
                // 將值轉(zhuǎn)換為數(shù)字
                const num = parseFloat(cleanedValue);
                
                // 實(shí)現(xiàn)印度數(shù)字分組系統(tǒng)
                const parts = Math.abs(num).toFixed(2).split('.');
                let integerPart = parts[0];
                const decimalPart = parts[1];
                
                // 處理整數(shù)部分的分組
                let lastThree = integerPart.substring(integerPart.length - 3);
                const otherNumbers = integerPart.substring(0, integerPart.length - 3);
                
                if (otherNumbers !== '') {
                    // 每兩位數(shù)字添加逗號
                    lastThree = ',' + lastThree;
                    let formattedOther = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",");
                    integerPart = formattedOther + lastThree;
                }
                
                // 添加符號和小數(shù)部分
                let result = (num < 0 ? '-' : '') + integerPart + '.' + decimalPart;
                
                return result;
            }
            
            // 格式化按鈕點(diǎn)擊事件
            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>

功能說明

這個工具提供了以下功能:

  • 數(shù)字格式化:將輸入的數(shù)字格式化為 #,###,##0.00 格式
  • 實(shí)時預(yù)覽:輸入數(shù)字后點(diǎn)擊"格式化"按鈕查看結(jié)果
  • 示例展示:提供多個格式化示例供參考
  • Java代碼:展示如何在Java中實(shí)現(xiàn)相同的格式化功能

使用說明

  • 在輸入框中輸入要格式化的數(shù)字
  • 點(diǎn)擊"格式化"按鈕或按Enter鍵查看結(jié)果
  • 格式化結(jié)果將顯示在結(jié)果區(qū)域
  • 可以參考下方的示例了解不同數(shù)字的格式化效果

這個工具模擬了Java中使用DecimalFormat類格式化BigDecimal數(shù)字的效果,特別是實(shí)現(xiàn)了印度數(shù)字分組系統(tǒng)(千、十萬等單位分組)。

到此這篇關(guān)于java構(gòu)建一個BigDecimal數(shù)字格式化工具的文章就介紹到這了,更多相關(guān)Java BigDecimal格式化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot與gRPC2.0集成的實(shí)現(xiàn)示例

    SpringBoot與gRPC2.0集成的實(shí)現(xiàn)示例

    本文主要介紹了SpringBoot與gRPC2.0集成,包括HTTP/2支持、ProtocolBuffers增強(qiáng)、服務(wù)定義改進(jìn)等內(nèi)容,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2026-04-04
  • Spring中@Async注解實(shí)現(xiàn)異步調(diào)詳解

    Spring中@Async注解實(shí)現(xiàn)異步調(diào)詳解

    在本篇文章里小編給大家分享的是關(guān)于Spring中@Async注解實(shí)現(xiàn)異步調(diào)詳解內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-04-04
  • Java中的@Repeatable注解使用詳解

    Java中的@Repeatable注解使用詳解

    這篇文章主要介紹了Java中的@Repeatable注解使用詳解,@Repeatable注解是java8為了解決同一個注解不能重復(fù)在同一類/方法/屬性上使用的問題,本文提供了解決思路和部分實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2024-02-02
  • Java排序算法總結(jié)之歸并排序

    Java排序算法總結(jié)之歸并排序

    這篇文章主要介紹了Java排序算法總結(jié)之歸并排序,較為詳細(xì)的分析了歸并排序的原理與java實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-05-05
  • springboot中引入日志文件生成的配置詳解

    springboot中引入日志文件生成的配置詳解

    本文主要介紹了springboot中引入日志文件生成的配置詳解,包括日志級別的設(shè)置、日志格式的配置以及日志輸出的位置等,從而幫助開發(fā)者更好地進(jìn)行開發(fā)與調(diào)試
    2023-10-10
  • 分析java中全面的單例模式多種實(shí)現(xiàn)方式

    分析java中全面的單例模式多種實(shí)現(xiàn)方式

    單例模式是一種常用的軟件設(shè)計(jì)模式,單例對象的類只能允許一個實(shí)例存在。許多時候整個系統(tǒng)只需要擁有一個的全局對象,有利于協(xié)調(diào)系統(tǒng)整體的行為。比如在某個服務(wù)器程序中,該服務(wù)器的配置信息存放在一個文件中。本文將介紹它的思想和多種實(shí)現(xiàn)方式
    2021-06-06
  • 解決BeanUtils.copyProperties不支持復(fù)制集合的問題

    解決BeanUtils.copyProperties不支持復(fù)制集合的問題

    這篇文章主要介紹了解決BeanUtils.copyProperties不支持復(fù)制集合的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Eclipse使用maven搭建spring mvc圖文教程

    Eclipse使用maven搭建spring mvc圖文教程

    這篇文章主要為大家分享了Eclipse使用maven搭建spring mvc圖文教程,感興趣的小伙伴們可以參考一下
    2016-05-05
  • SpringBoot中自動配置原理解析

    SpringBoot中自動配置原理解析

    SpringBoost是基于Spring框架開發(fā)出來的功能更強(qiáng)大的Java程序開發(fā)框架,本文將以廣角視覺來剖析SpringBoot自動配置的原理,涉及部分Spring、SpringBoot源碼,需要的可以參考下
    2023-11-11
  • Spring Boot 結(jié)合 aop 實(shí)現(xiàn)讀寫分離

    Spring Boot 結(jié)合 aop 實(shí)現(xiàn)讀寫分離

    這篇文章主要介紹了Spring Boot 結(jié)合 aop 實(shí)現(xiàn)讀寫分離的示例,幫助大家更好的理解和使用Spring Boot框架,感興趣的朋友可以了解下
    2020-11-11

最新評論

边坝县| 鹤庆县| 化隆| 静乐县| 浦城县| 新竹县| 博湖县| 太和县| 四会市| 卫辉市| 仙游县| 永仁县| 平塘县| 扶余县| 女性| 子长县| 襄垣县| 中超| 大邑县| 大兴区| 长乐市| 杭锦后旗| 汉中市| 洪雅县| 育儿| 九龙坡区| 工布江达县| 家居| 铜山县| 东乡族自治县| 前郭尔| 玉山县| 若羌县| 墨脱县| 从化市| 金阳县| 肇东市| 大石桥市| 博客| 陇西县| 凌云县|