简体   繁体   English

不删除Java大十进制中的前导零

[英]Not removing leading zero's in Big Decimal in java

I want to write 5 digit zip code to an Excel file. 我想将5位邮政编码写入Excel文件。 for ex. 对于前。 for number 01803 if I don't convert it to string it will store it as 01803 string but shows an error on excel file.So I want to store it as Big Decimal number without stripping the leading 0's.but Big decimal by default strips it for the current case. 对于数字01803如果我不将其转换为字符串,它将存储为01803字符串,但是在excel文件中显示错误。所以我想将其存储为Big Decimal数字而不剥离前导0。对于当前情况。

In excel, when I store it as string and convert that to number in excel it will strip the leading zero's 在excel中,当我将其存储为字符串并将其转换为excel中的数字时,它将去除前导零

    Address address = new Address();
    address.setPostalCode("01803");
    String postCode = address.getPostalCode();
    if(postCode.matches("^[0-9]*$") && postCode.length() > 2){
        if(postCode.charAt(0) == '0'){
            System.out.println(postCode.toString());    
        } else {
            System.out.println(new BigDecimal(postCode).doubleValue());                         
        }
    }else {
        System.out.println(address.getPostalCode());
    }

Can give this a try 可以试试看

 String abc = 0003;
 formatToCVSValue(abc);

    public static String formatToCVSValue(String s)
        {
            if(s==null || !hasLength(s))
                return "";
            else
            {
                return "\"=\"\""+s+"\"\"\"";
            }
        }

When open in excel, you will see exactly 0003 instead of '0003 在excel中打开时,您将看到完全是0003而不是'0003

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM