货币格式处理,Java,

import java.text.NumberFormat;
import java.util.Locale;

public class test {

    /**
     * @param args
     */
    /*
     * public static void main(String[] args) {
     *
     * //System.out.println(String.valueOf((Double.valueOf("49.421123")*Double.
     * valueOf("1"))).trim());
     *
     * String aa=""; String bb=""; System.out.println(aa.equals(bb)); }
     */

    public static void main(String[] args) {
        // 不使用格式化输出数
        double d = 10000.0 / 3.0;
        System.out.println("无格式化输出:" + d);
        // 使用本地默认格式输出数
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        // numberFormat.setMaximumFractionDigits(4);
        // numberFormat.setMinimumIntegerDigits(6);
        String numberString = numberFormat.format(d);
        System.out.println("本地默认格式输出数:" + numberString);
        // 使用本地默认格式输出货币值
        NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
        System.out.println("本地默认格式输出货币值:" + currencyFormat.format(d));
        // 使用本地默认格式输出百分数
        NumberFormat percentFormat = NumberFormat.getPercentInstance();
        System.out.println("本地默认格式输出百分数:" + percentFormat.format(d));
        // 在不同的国家和地区数字表示的格式也有区别。如德国
        // 使用德国的格式化输出数
        NumberFormat numberFormatG = NumberFormat
                .getNumberInstance(Locale.GERMANY);
        System.out.println("德国数字输出形式:" + numberFormatG.format(d));
        // 使用德国货币输出形式
        NumberFormat currencyFormatG = NumberFormat
                .getCurrencyInstance(Locale.GERMANY);
        System.out.println("德国货币输出形式:" + currencyFormatG.format(d));
        // 使用美国货币输出形式
        NumberFormat currencyFormatA = NumberFormat
                .getCurrencyInstance(Locale.US);
        System.out.println("美国货币输出形式:" + currencyFormatA.format(d));
        // 使用德国百分数输出形式
        NumberFormat percentFormatG = NumberFormat
                .getPercentInstance(Locale.GERMANY);
        System.out.println("德国百分数输出形式:" + percentFormatG.format(d));
        System.exit(0);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要读取格式化的Excel文件,需要使用Java中的Apache POI库。Apache POI库是一个用于读写微软Office格式文件(如Excel、Word和PowerPoint等)的Java API。 下面是一个示例代码,演示如何使用Apache POI库读取Excel文件并获取格式化数据: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class ExcelReader { public static void main(String[] args) { try { FileInputStream file = new FileInputStream(new File("data.xlsx")); Workbook workbook = WorkbookFactory.create(file); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { if (cell.getCellType() == CellType.NUMERIC) { System.out.print(cell.getNumericCellValue() + "\t"); } else if (cell.getCellType() == CellType.STRING) { System.out.print(cell.getStringCellValue() + "\t"); } } System.out.println(); } file.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 这个例子中,我们首先通过FileInputStream将Excel文件读入内存中,然后使用WorkbookFactory创建Workbook对象。接着,我们获取第一个Sheet的引用,并使用两个for循环遍历所有行和列。对于每个单元格,我们检查其类型并分别处理数字和字符串。最后,我们关闭文件输入流并输出结果。 需要注意的是,Excel中的单元格可以有不同的格式,例如日期、货币、百分比等。如果要读取这些格式化数据,需要使用Apache POI库中的相应类进行解析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

[shenhonglei]

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值