简体   繁体   English

Java字符串新行,具有特定逻辑且不包含字符串格式

[英]Java String new line with specific logic and without string format

The following code: 如下代码:

String a = "100.00";
String b = "10.00";
String c=  "5.00";

String value = a+ "\n"+ b +"\n" +c;
System.out.println(value);

Prints: 打印:

100.00
10.00
5.00

I need output in a format where decimal point position will be fixed without using any string format library (with logic): 我需要以不固定任何字符串格式库(带逻辑)的小数点位置固定的格式输出:

100.00
 10.00
  5.00 

Variable values are coming from the database and requirement is to show values with the decimal point in the same position with values vertically. 变量值来自数据库,要求显示的小数点在同一位置,垂直显示。

Here's an example using streams; 这是一个使用流的示例; probably could be more efficient but I was having fun. 可能会更有效率,但是我很开心。

It assumes all have 2 decimals as you showed, just FYI; 假设您所显示的所有小数点均为2位,仅供参考。 that could be modified though. 可以修改。

String a = "100.00";
String b = "10.00";
String c=  "5.00";

List<String> strings = Arrays.asList(a, b, c);
final int maxLen = strings.stream().map(String::length).reduce(Math::max).get();

strings.forEach(s -> {
    System.out.println(Stream.generate(() -> " ").limit(maxLen - s.length()).collect(Collectors.joining()) + s);
});

Results: 结果:

100.00
 10.00
  5.00

I also put pthe 3 examples into a list so it would work on an arbitrary number of elements. 我还将pthe 3示例放入列表中,以便可以在任意数量的元素上使用。

Without Java 8 没有Java 8

String a = "100.00";
String b = "10.00";
String c =  "5.00";

List<String> strings = Arrays.asList(a, b, c);

int maxLen = -1;
for (String s : strings) maxLen = Math.max(maxLen, s.length());
for (String s : strings) {
    String spaces = "";
    for (int i = 0; i < maxLen - s.length(); ++i) {
        spaces += " ";
    }
    System.out.println(spaces += s);
}

I don't know what 'don't use any String format library' means specifically, but... now we get into a semantic argument as to what 'library' means. 我不知道“不使用任何String格式库”的具体含义,但是...现在,我们来了解一下“库”的含义的语义论证。 You're formatting a string. 您正在格式化字符串。 If I take your question on face value you're asking for literally the impossible: How does one format a string without formatting a string? 如果我对您的面值提出疑问,那么您实际上是在问不可能的问题:一个人如何格式化字符串而不格式化字符串?

I'm going to assume you meant: Without adding a third party dependency. 我将假设您的意思是:不添加第三方依赖项。

In which case, if you have doubles (or BigDecimal or float ) as input: 在这种情况下,如果您将doubles (或BigDecimalfloat )作为输入:

String.format("%6.2f", 100.00);
String.format("%6.2f", 10.00);
String.format("%6.2f", 5.00);

(6? Yes; 6! The 6 is the total # of characters to print at minimum. You want 3 digits before the separator, a separator, and 2 digits after, that's a grand total of 6, hence the 6.) (6?是; 6!6是最少要打印的总字符数。您要在分隔符前加上3位数字,然后在分隔符后加上2位数字,总共要有6个数字,因此是6。)

If you have strings as input, evidently you are asking to right-justify them? 如果您将字符串作为输入,那么显然您是在要求对它们进行右对齐吗? Well, you can do that: 好吧,你可以这样做:

String.format("%6s", "100.00");
String.format("%6s", "10.00");
String.format("%6s", "5.00");

NB: For convenience, System.out.printf("%6s\\n", "100.0"); 注意:为方便起见, System.out.printf("%6s\\n", "100.0"); is short for System.out.println(String.format("%6s", "100.0")); System.out.println(String.format("%6s", "100.0"));缩写System.out.println(String.format("%6s", "100.0"));

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

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