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 4320522
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:38:37+00:00 2026-05-21T08:38:37+00:00

I am using DecimalFormat to create a formated decimal that is always 6 characters

  • 0

I am using DecimalFormat to create a formated decimal that is always 6 characters long. At first I used the format string of new DecimalFormat("000.00") but this gave me a bug for negative numbers. The minus sign is added and makes the number one space larger resulting in -005.25 and not -05.25 as desired.

I have been able to fix this with the following code

DecimalFormat fmt;  
if(netAmt < 0){  
    fmt = new DecimalFormat("00.00");  
}else{  
    fmt = new DecimalFormat("000.00");  
}  

System.out.println(fmt.format(netAmt));

But DecimalFormat has the ; character to format negative numbers differently then positive numbers. I have not been able to get this work correctly. As I understand it the following code should work just like the above.

DecimalFormat fmt = new DecimalFormat("000.00;00.00");  

System.out.println(fmt.format(netAmt));

The result is that the pattern before the ; is used for both negative and positive numbers causing the -005.25 error to remain. What am I doing wrong? Am I misunderstanding what ; is for?

  • 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-21T08:38:37+00:00Added an answer on May 21, 2026 at 8:38 am

    Does the following string pattern help you: "%06.2f%n"
    A fixed width of 6 with “0” padding the front?

    example

    System.out.println(String.format("%06.2f%n",1.3));
    System.out.println(String.format("%06.2f%n",-3.323));
    

    What do you want the behavior to be when the number is greater than 3 digits, ie won’t fit?

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

Sidebar

Related Questions

I'm using DecimalFormat to format doubles to 2 decimal places like this: DecimalFormat dec
Is it possible to format price according to rules like this using DecimalFormat in
I'm trying to format a BigDecimal value by using methods of DecimalFormat.format(). My problem,
I have a DecimalFormat object which I'm using to format all of my double
How do I round a double to 5 decimal places, without using DecimalFormat ?
I have the following decimal format previously : private static final DecimalFormat decimalFormat =
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using C#, I need a class called User that has a username, password, active
I have a java class that I use to create a text file from

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.