Java中如何用下划线填充数字?

发布于 2024-11-27 23:27:28 字数 2132 浏览 0 评论 0原文

如何在Java中用下划线填充数字,而不是通常的零?

例如,我希望

  • 将 123.45 格式化为 ___123.45,
  • 将 12345.67 格式化为 _12345.67
  • 0.12 格式化为 ______.12

我尝试了很多东西,最接近的是这个(通过使用SYMBOLS.setZeroDigit('_');):

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class Example {

    public static void main(String[] args) {
        DecimalFormatSymbols SYMBOLS = new DecimalFormatSymbols();
        SYMBOLS.setDecimalSeparator('.');
        SYMBOLS.setGroupingSeparator(',');
        DecimalFormat OUTPUT_FORMAT = new DecimalFormat("000,000.00", SYMBOLS);

        System.out.println(OUTPUT_FORMAT.format(0.01));
        // got 000,000.01
        System.out.println(OUTPUT_FORMAT.format(0.12));
        // got 000,000.12
        System.out.println(OUTPUT_FORMAT.format(123456));
        // got 123,456.00
        System.out.println(OUTPUT_FORMAT.format(123456.78));
        // got 123,456.78
        System.out.println(OUTPUT_FORMAT.format(1234));
        // got 001,234.00
        System.out.println(OUTPUT_FORMAT.format(1234.56));
        // got 001,234.56

        SYMBOLS.setZeroDigit('_');
        OUTPUT_FORMAT = new DecimalFormat("000,000.00", SYMBOLS);
        System.out.println(OUTPUT_FORMAT.format(0.01));
        // expected ______._1 but got ___,___._`
        System.out.println(OUTPUT_FORMAT.format(0.12));
        // expected ______.12 but got ___,___.`a
        System.out.println(OUTPUT_FORMAT.format(123456));
        // expected 123,456.__ but got `ab,cde.__
        System.out.println(OUTPUT_FORMAT.format(123456.78));
        // expected 123,456.78 but got `ab,cde.fg
        System.out.println(OUTPUT_FORMAT.format(1234));
        // expected __1,234.00 or at least __1,234.__ but got __`,abc.__
        System.out.println(OUTPUT_FORMAT.format(1234.56));
        // expected __1,234.56 but got __`,abc.de
    }

}

好吧,如果格式正确的话,不是很接近,而是一个空数字(带有尾随下划线): ________

无论如何,关于如何获得预期行为的建议?

How to pad numbers with underscores in Java, instead of the usual zeros ?

For example I want

  • 123.45 to be formated to ___123.45 and
  • 12345.67 to be formated to _12345.67
  • 0.12 to be formated to ______.12

I tried lots of stuff and closest I got was this (by using SYMBOLS.setZeroDigit('_');):

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

public class Example {

    public static void main(String[] args) {
        DecimalFormatSymbols SYMBOLS = new DecimalFormatSymbols();
        SYMBOLS.setDecimalSeparator('.');
        SYMBOLS.setGroupingSeparator(',');
        DecimalFormat OUTPUT_FORMAT = new DecimalFormat("000,000.00", SYMBOLS);

        System.out.println(OUTPUT_FORMAT.format(0.01));
        // got 000,000.01
        System.out.println(OUTPUT_FORMAT.format(0.12));
        // got 000,000.12
        System.out.println(OUTPUT_FORMAT.format(123456));
        // got 123,456.00
        System.out.println(OUTPUT_FORMAT.format(123456.78));
        // got 123,456.78
        System.out.println(OUTPUT_FORMAT.format(1234));
        // got 001,234.00
        System.out.println(OUTPUT_FORMAT.format(1234.56));
        // got 001,234.56

        SYMBOLS.setZeroDigit('_');
        OUTPUT_FORMAT = new DecimalFormat("000,000.00", SYMBOLS);
        System.out.println(OUTPUT_FORMAT.format(0.01));
        // expected ______._1 but got ___,___._`
        System.out.println(OUTPUT_FORMAT.format(0.12));
        // expected ______.12 but got ___,___.`a
        System.out.println(OUTPUT_FORMAT.format(123456));
        // expected 123,456.__ but got `ab,cde.__
        System.out.println(OUTPUT_FORMAT.format(123456.78));
        // expected 123,456.78 but got `ab,cde.fg
        System.out.println(OUTPUT_FORMAT.format(1234));
        // expected __1,234.00 or at least __1,234.__ but got __`,abc.__
        System.out.println(OUTPUT_FORMAT.format(1234.56));
        // expected __1,234.56 but got __`,abc.de
    }

}

Well, not really close but an empty number if formated right (with trailing underscores):
___,___.__

Anyway, suggestions on how to get the expected behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我早已燃尽 2024-12-04 23:27:28
"_________".substring( num.length() )+num;

其中 num 是保存您的号码的 String。要将 double 转换为 String,您可以使用 Double.toString(myDouble)

"_________".substring( num.length() )+num;

where num is a String holding your number. To convert a double to a String, you can use Double.toString(myDouble).

等你爱我 2024-12-04 23:27:28

您可以使用 来自 Apache Commons 库的 StringUtils.leftPad() 方法。

// pad to 10 chars
String padded = StringUtils.leftPad("123.456", 10, '_');
// padded will be equal to "___123.456"

You can use the StringUtils.leftPad() method from the Apache Commons library.

// pad to 10 chars
String padded = StringUtils.leftPad("123.456", 10, '_');
// padded will be equal to "___123.456"
孤檠 2024-12-04 23:27:28

Da daaa

import java.text.DecimalFormat;

class F { 
  public static void main( String ... args ) { 
    DecimalFormat f = new DecimalFormat("000,000.00");
    for( double d : new double[]{ 0.01, 0.12, 123456, 123456.78, 1234, 1234.56 }){ 
      System.out.println( 
        f.format(d).replaceAll("0","_").replaceAll("_,_","__")
      );
    }
  }
}

输出:

______._1
______.12
123,456.__
123,456.78
__1,234.__
__1,234.56

也许替换可以一步完成,但我会把它交给你。

Da daaa

import java.text.DecimalFormat;

class F { 
  public static void main( String ... args ) { 
    DecimalFormat f = new DecimalFormat("000,000.00");
    for( double d : new double[]{ 0.01, 0.12, 123456, 123456.78, 1234, 1234.56 }){ 
      System.out.println( 
        f.format(d).replaceAll("0","_").replaceAll("_,_","__")
      );
    }
  }
}

Output:

______._1
______.12
123,456.__
123,456.78
__1,234.__
__1,234.56

Probably the replacement could be done in one step but I'll let that to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文