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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:20:52+00:00 2026-05-28T05:20:52+00:00

I am doing a college assignment in Java that deals with currency. For that

  • 0

I am doing a college assignment in Java that deals with currency. For that I am advised to use ints instead of doubles and then later convert it to a dollar value when I print out the statement.

Everything works fine until I do calculations on the number 4005 (as in $40.05 represented as an int). I am pasting the part of code I am having problems with, I would appreciate if someone could tell me what I am doing wrong.

import java.io.*;
class modumess {
    public static void main(String[] args) {
        int money = 4005; //Amount in cents, so $40.05;

        // Represent as normal currency
        System.out.printf("$%d.%d", money/100, money%100);
    }
}

The above code, when run, shows $40.5, instead of $40.05. What gives?

Kindly note that this is for my homework and I want to learn, so I would really appreciate an explanation about the root of the problem here rather than just a simple solution.

EDIT: Following Finbarr’s answer, I have added the following to the code which seems to have fixed the problem:

if (money%100 < 10) {
            format = "$%d.0%d";
        }

Is this a good way to do it or am I over-complicating things here?

EDIT: I just want to make it clear that it was both Finbarr and Wes’s answer that helped me, I accepted Wes’s answer because it made it clearer for me on how to proceed.

  • 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-28T05:20:52+00:00Added an answer on May 28, 2026 at 5:20 am

    A better way would be something like this for a general case:

    format = "%d.%02d";
    

    %02d gives you 0 padding for 2 digits. That way you don’t need the extra if statement.

    See this for more explanation of things you can do in format: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax

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

Sidebar

Related Questions

Currently doing a group project for college in Java. The assignment is to produce
I'm a college student doing a Java homework. I've created this program that allows
I have the following code that I am working on for my college assignment.
I am learning SQL and DB design for a college class. One assignment that
I'm doing a college assignment in XNA to make a virtual piano. I have
Hey! I am currently doing some prep for my upcoming college assignment but am
I am doing college project in winform, mysql ,c#. for that I have created
i am doing a library project for my college. in that, i have table
When doing case-insensitive comparisons, is it more efficient to convert the string to upper
When doing TDD , how to tell that's enough tests for this class /

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.