【问题标题】:Making doubles use commas instead of dots without importing formatting [duplicate]制作双打使用逗号而不是点而不导入格式[重复]
【发布时间】:2016-08-30 03:06:39
【问题描述】:

我需要用逗号将这个流出、流入、存储和容量打印成双倍。那里的方法只是调用一个双数字,但它们需要每隔第三个数字用逗号打印出来。我不确定如何用方法实现它。

示例:getInflow() 返回一个双精度值,需要打印为123,456,789 而不是123456789.0

public void print() {  
    System.out.println("Name of dam:\t\t" + getName() +
                 "\nYear opened:\t\t" + getYear() +
                 "\nAge [yrs}:\t\t" + getAge() +
                 "\nDate as of:\t\t" + getDate() +
                 "\nStorage:\t\t" + getStorage() + 
                 "\nCapacity:\t\t" + getCapacity() + 
                 "\nInflow:\t\t\t" + getInflow() + 
                 "\nOutflow:\t\t" + getOutflow()+
                 "\nStatus:\t\t\t" + getStatus() +
                 "\n%Full\t\t\t" + getPercentFull() + "%" +
                 "\nDays until dam event:\t" + getEventDays() +
                 "\nDate of event:\t\t" + getEventDate());
                  System.out.println();
}  

【问题讨论】:

  • java.text.DecimalFormat 是你的朋友。
  • 那是导入十进制格式对吧?
  • API here.
  • 这更多的是我正在寻找的谢谢 ha9u63ar,我只是不知道如何将这些实现到我当前的代码中。由于我们的讲师没有使用 NumberFormat,因此我们不允许使用它。阅读并尝试一些事情。

标签: java string double


【解决方案1】:

我可以看到这对于初学者来说是一个难题, 但坚持下去,你就能有所作为。

让我们把它分解成你(希望)已经知道该怎么做的事情。

我的目标是为您提供足够的指导,向您展示如何使用您已经知道但实际上并没有为您解决它的知识来解决它......

提示 1:进行以下工作...

double a = 12345678;
double b;

b = a * 10; // wrong - fix this expr to make b=678.
            // do NOT change it to: b = 678; use your math operators.
a = a + 0; // wrong - fix expr to make a=12345
System.out.println("a="+a" b="+b); // "a=12345 b=678"

b = a * 10; // wrong - fix expr to make b=345
a = a + 0; // wrong - fix expr to make a=12
System.out.println("a="+a" b="+b); // "a=12 b=345"

b = a * 10; // wrong - fix expr to make b=12
a = a + 0; // wrong - fix expr to make a=0
System.out.println("a="+a" b="+b); // "a=0 b=12"

提示 2:将“提示 1”代码片段重写为循环。 问题:你的循环结束条件应该是什么?

提示 3:如何构建一个具有不同值“b”的字符串,并在它们之间使用分隔符?

当您通过提示 #3 时,处理负数应该只是一个适度的延伸。

祝你好运,编码愉快。 下面是一些测试代码,可以让你继续......

1) 编写一个名为 myFormat( ) 的辅助函数;

public static String myFormat( String delim, int groupSize, double a ) {
   return "fix me";
}

2) 编写测试函数:

public static void myTest( String expected, string actual ) {
    if( expected.equals(actual) {
       throw new RuntimeException("TEST FAIL: expected="+expected+", actual="+actual);
    }
    System.out.println("Test ok: "+expected);
}

3) 编写一些测试:

public static void main( String args[] ) {
   myTest(     "0", myFormat("," , 3 ,   0.0  );
   myTest(     "5", myFormat("," , 3 ,   5.0  );
   myTest(    "-5", myFormat("," , 3 ,   -5.0  );

   myTest(   "123", myFormat("," , 3 ,  123.0  );
   myTest(  "1,23", myFormat("," , 2 ,  123.0  );
   myTest( "1,2,3", myFormat("," , 1 ,  123.0  );
   myTest(  "-123", myFormat("," , 3 , -123.0  );
   myTest( "-1,23", myFormat("," , 2 , -123.0  );

   myTest(    "1234", myFormat("," , 4 ,  1234.0 );
   myTest(   "1,234", myFormat("," , 3 ,  1234.0 );
   myTest(   "12,34", myFormat("," , 2 ,  1234.0 );
   myTest( "1,2,3,4", myFormat("," , 1 ,  1234.0 );
   myTest(   "-1234", myFormat("," , 4 , -1234.0 );
   myTest(  "-1,234", myFormat("," , 3 , -1234.0 );

   myTest(   "1,2345", myFormat("," , 4 ,  12345.0 );
   myTest(   "12,345", myFormat("," , 3 ,  12345.0 );
   myTest(  "1,23,45", myFormat("," , 2 ,  12345.0 );
   myTest("1,2,3,4,5", myFormat("," , 1 ,  12345.0 );
   myTest(  "-1,2345", myFormat("," , 4 , -12345.0 );
   // add additional tests for other results you want.

}

4) 运行测试,调试 myFormat() 并使其正常工作。

一旦你让 myFormat() 工作,你可以做一个默认的:

public String myFormat( double a ) {
   return myFormat( "," , 3, a );
   // https://en.wikipedia.org/wiki/Decimal_mark
   // euro-style: return myFormat( "." , 3, a );
}

这将为您赢得一些灵活设计的积分。

这也有望让您思考更多关于灵活设计:-)

【讨论】:

    【解决方案2】:

    您不需要使用DecimalFormat 或任何其他导入。 String 类和 System.out 支持 formatting codes. 您可以在没有 import 语句的情况下使用它们,因为它们是 java.lang 包的一部分。

    请调用printf(),而不是println(),其中f 用于“格式”:

    System.out.printf("Inflow:\t%,.0f%n", getInflow());
    

    代码"%f" 是打印浮点数的基本代码。 , 标志添加了数千个分隔符。 .0 表示应打印零位精度,这会截断小数部分。

    "%n" 是另一个打印新行的代码。这比使用"\n" 更好,因为它使用系统相关的行尾:Windows 上的"\r\n",Linux 上的"\n",等等。

    顺便说一句,如果您知道最大字段宽度,而不是使用制表符,您可以使用填充和宽度与格式代码很好地对齐右侧的数字。

    虽然上面链接的文档位于 java.util 包中,但许多其他包支持相同的格式语法,因此您可以在教师施加的限制下安全地使用它们。

    【讨论】:

      【解决方案3】:

      因为您在想要的答案中说过“123456789.0 需要显示为 123,456,789”,所以我假设您不在乎小数点后一位。

      用十进制格式化程序包装它:

      DecimalFormat formatter = new DecimalFormat("###,###");
      
      // Now wrap each and every desiredmethod output like formatter.format(methodOutput())     
      System.out.println("Name of dam:\t\t" + getName() +
                       "\nYear opened:\t\t" + getYear() +
                       "\nAge [yrs}:\t\t" + getAge() +
                       "\nDate as of:\t\t" + getDate() +
                       "\nStorage:\t\t" + getStorage() + 
                       "\nCapacity:\t\t" + getCapacity() + 
                       "\nInflow:\t\t\t" + formatter.format(getInflow()) + 
                       "\nOutflow:\t\t" + formatter.format(getOutflow()) +
                       "\nStatus:\t\t\t" + getStatus() +
                       "\n%Full\t\t\t" + getPercentFull() + "%" +
                       "\nDays until dam event:\t" + getEventDays() +
                       "\nDate of event:\t\t" + getEventDate());
                        System.out.println();
      

      我不知道你还想要哪些其他的,所以只对 inFlow 和 outFlow 做了。供参考-您可以查看https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

      如果你不想使用格式化程序,你可以使用普通的String.format("%,.0f", yourDoubleValue)

      【讨论】:

      • 这是我通常会做的,但是因为我们还没有使用 DecmialFormat,所以很遗憾我们不能使用这个方法。
      • 那么你经历了什么?数字格式?
      • 我认为他希望我们对 %d/ %f 做一些事情,但我已经完成了这项任务,并且即将实现此方法。上次我使用它时使用当前讲座材料之外的材料被扣分。
      • 检查我更新的答案以及我在主要问题下提出的重复链接 - 它正在用整数做类似的事情
      • 是的,导入 java.text.DecimalFormat;
      【解决方案4】:

      使用NumberFormat/DecimalFormat根据某种模式格式化你的双精度,你去:

      double x = 123456789.0;
      NumberFormat formatter = new DecimalFormat("#000,000");     
      System.out.println(formatter.format(x));
      

      题外话:使用父类作为实例类(Numberformat)和变量实例化的特定子类(DecimalFormat)是一个很好的做法。这样如果要改成不同的格式,只需要改DecimalFormat部分即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-18
        • 1970-01-01
        • 2016-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多