Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6150021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:33:07+00:00 2026-05-23T19:33:07+00:00

Two questions in one, but I have a very short test case demonstrating my

  • 0

Two questions in one, but I have a very short test case demonstrating my problems:

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.imageio.*;

public class Numbers {

    public static void main(String[] args) throws IOException {
        double[] numbers = { 10000.12, 20000.23, 3000.45 };
        DecimalFormat format = new DecimalFormat("0.00");
        format.setGroupingUsed(false);
        BufferedImage image = new BufferedImage(400, 150, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        g2d.setColor(Color.BLACK);
        g2d.setBackground(Color.YELLOW);
        g2d.clearRect(0, 0, image.getWidth(), image.getHeight());
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setFont(new Font(Font.SANS_SERIF,Font.BOLD, 24));
        FontMetrics metrics = g2d.getFontMetrics();

        for (int i = 0; i < numbers.length; i++) {
            String str = format.format(numbers[i]);
            System.out.println(i + ": " + str);

            int w = metrics.stringWidth(str);
            int h = metrics.getHeight();
            int x = 100 * i;
            g2d.drawString(str, x - w/2, 0);
        }

        g2d.dispose();
        ImageIO.write(image, "PNG", new File("Numbers.png"));
    }
}

When I use it I get:

C:\Temp>javac -version
javac 1.6.0_24

C:\Temp>javac Numbers.java

C:\Temp>java Numbers
0: 10000,12
1: 20000,23
2: 3000,45

and this image produced:

Numbers.png

My questions:

  1. How to force the DecimalFormat to use dot and not comma, without switching locale?
  2. The w seems to be correct, but what is wrong with 0 as the height – why are numbers printed offscreen?

Thank you! Alex

UPDATE:

I’ve ended up using Locale. The str.replace(“.”, “,”) comment is cool too.

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.imageio.*;

public class Numbers {

    public static void main(String[] args) throws IOException {
        double[] numbers = { 10000.12, 20000.23, 3000.45 };
        BufferedImage image = new BufferedImage(400, 150, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();
        g2d.setColor(Color.WHITE);
        g2d.setBackground(Color.GRAY);
        g2d.clearRect(0, 0, image.getWidth(), image.getHeight());
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setFont(new Font(Font.SANS_SERIF,Font.BOLD, 24));
        FontMetrics metrics = g2d.getFontMetrics();
        NumberFormat format = NumberFormat.getInstance(Locale.US);
        format.setGroupingUsed(false);
        if (format instanceof DecimalFormat) {
            ((DecimalFormat) format).applyPattern("0.00");
        }

        for (int i = 0; i < numbers.length; i++) {
            String str = format.format(numbers[i]);
            System.out.println(i + ": " + str);

            int w = metrics.stringWidth(str);
            int h = metrics.getHeight();
            int x = 100 * i;
            g2d.drawString(str, x - w/2, h);
        }

        g2d.dispose();
        ImageIO.write(image, "PNG", new File("Numbers.png"));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:33:07+00:00Added an answer on May 23, 2026 at 7:33 pm

    For 1: You can specify the format using format.applyLocalizedPattern("#00.0#"); instead of format.setGroupingUsed(false);

    Another option is to use custom format symbols:

    DecimalFormatSymbols decimalSymbol = new DecimalFormatSymbols(Locale.getDefault());
    decimalSymbol.setDecimalSeparator('.');
    format.setGroupingUsed(false);
    

    More information about localized patterns and some examples can be found here:
    http://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html and http://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html#numberpattern

    For your second question, When you use g2d.drawString(str, x, y) the baseline is at x,y not the top left position as when you draw a rectangle. The baseline is basically the bottom of letters like a, b, c. Commas and letters like g, j, y extend below the baseline. You basically need to add the height of the text to the y position where you want to display the text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've seen a very few questions like this one but none have a solution
I have two related questions one general and one specific to the project I'm
This is two questions in one, so here we go. I have a simple
I have two Questions. One small and one greater problem. Why i have to
I actually have two questions, the on in the title being the main one.
This is sort of two coding style questions in one. I have a tab-dl'd
I actually have two questions regarding the same problem but I think it is
Note: this question is related to this one , but two years is a
I've got two questions (of which at least one is regarding RTTI in D2010
The question has two parts, one of which I already have the answer for.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.