Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Formating integers into money

  1. #1

    Default Formating integers into money

    having the following fields for my money class.
    import java.text.DecimalFormat;
    public class Money 
    {
    		//Fields for money will hold dollars and cents
    	private long dollars;
    	private long cents;

    My task is to use those fields and make a toString method that returns them like a dollars sign. For instance, if there are 32 dollars and 40 cents, then in my String method I have to return something similar to this "$32.40."

    I have already tried some of the methods, but they don't seem to work fine.
    public String toString()
    	{
    		DecimalFormat formatter=new DecimalFormat("$#0");
    		DecimalFormat formatCents=new DecimalFormat(".00");
    		return formatter.format(dollars)+ formatCents.format(cents);
    	}

    Anyone willing to lend an idea?

    --- Update ---

     import java.text.DecimalFormat;
    public class Money 
    {
    		//Fields for money will hold dollars and cents
    	private long dollars;
    	private long cents;
     
    	//Create a constructor initlizing the fields
    	public Money(double money)
    	{
    		if(money<0)
    			System.out.print("Negative Amount cannot be accepted");
    		else
    		{
    		long total_cents=(long)money*100;
    		dollars=total_cents/100;
    		cents=total_cents%100;
    		}
    	}
    	//Return a toString method that returns a string
    	public String toString()
    	{
    		DecimalFormat formatter=new DecimalFormat("$#00.00");
    		long total_Cents=(dollars*100)+cents;
    		return formatter.format(total_Cents);
    	}

    Actually changed a little on my code and I believe strongly this should work; however, doesn't seem to. In my demo,
    public class Dem
    {
     
     
    	public static void main(String[] args) 
    	{
    		Money myMoney=new Money(7.10);
    		System.out.print(myMoney.toString());
     
    	}
     
    }
    I pass this, but I get "$700.00" as the answer... confused...

    --- Update ---

    nevermind, my calculation is wrong in the toString method, but still the cents do not appear to be showing.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Formating integers into money

    you have a little problem with this statement in your constructor
    long total_cents=(long)money*100;
    dollars=total_cents/100;
    cents=total_cents%100;


    Here's another way.
    let say I'll pass 7.45 (as double primitive type)
    First, I'll get the dollar which is 7
    I'll just equate it to int or long primitive type. that way, I'll get rid of .45
    ex:
    int dollar = (int) 7.45; or
    long dollar = (long) 7.45;
    that way, variable dollar is equal to 7

    next I'll get the modulo of 7.45 in 1. that way, I can get the decimal place of the number
    int cent = 7.45 % 1; or
    long cent = 7.45 % 1;
    that way, the value of cent equal to 45. you just need to format it in some case, in this case the modulo is 4500...02.

    then return it
    return "$" + dollar + " cent" + cent;

    of course there is still another way of doing that.

    --- Update ---

    long total_cents=(long)money*100; -- casting it to long first (7.10 becomes 7) then multiply it to 100 (becomes 700)
    dollars=total_cents/100; -- 700 divided by 100 is 7 (just getting the dollar value).
    cents=total_cents%100; -- you are just getting the remainder of the dollar when divided by 100.

    you can do this
    long total_cents=(long) (money*100); -- as you noticed, I put parenthesis in the multiplication so it means to multiply first before casting to long

Similar Threads

  1. Help with a for loop and formating
    By Oldemor in forum Java Theory & Questions
    Replies: 7
    Last Post: October 20th, 2013, 06:47 AM
  2. [SOLVED] Java formating
    By maple1100 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 14th, 2013, 07:42 PM
  3. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  4. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM