-1

So the title says it all, Ive got a problem when trying to type out a larg number using the g.drawstring but im just getting an E in my number.

What can i do to prevent this?

Thanks / Code

static double TOC=14214111;
   .....

 f = new Font("Arial", Font.BOLD, 16);
       .....

     protected void paintComponent ( Graphics g ) {
     super.paintComponent ( g );
     g.setFont(f);
     g.drawString(""+ TOC, 280, 34);

        repaint();
     }

      ....

Basicaly some code, just ask and i will upload the whole code if u need see it.

  • 2
    "So the title says it all" - to be honest, if I had just the title I didn't get your problem. As I understand you you need the number formatted to a string without scientific notation. That's totally unrelated to drawing it somewhere as `Graphics#drawString()` doesn't know what the string means so it draws whatever is in that string. To format a number, have a look at `NumberFormat`, `String.format(...)` etc. – Thomas Jan 25 '18 at 10:09
  • `"" + ((long)TOC)` will do if the number is not too big. – maraca Jan 25 '18 at 10:11

1 Answers1

1

Do the formatting yourself:

g.drawString(String.format("%.0f", TOC)) // Change "%.0f" to whatever you prefer

Or, as suggested by @trashgod, use NumberFormat with proper options:

NumberFormat fmt = NumberFormat.getNumberInstance();
fmt.setRoundingMode(RoundingMode.HALF_EVEN);
fmt.setMaximumFractionDigits(3);
g.drawString(fmt.format(12345678901234.12356)); // 12,345,678,901,234.123
Matthieu
  • 2,736
  • 4
  • 57
  • 87