Results 1 to 7 of 7
  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Change a double value

    Hi,

    I am building a small accounting program that I will import transactions from a downloaded bank file.
    Most of my code is getting pretty good, but I have one problem:
    When I import the data from the file, the amounts in the file are either positive or negative (in or out)
    Now inside my program I want to make 2 transactions: One negative and one positive.
    So the question is: How can i change the amount from being positive to negative or negative to positive based on the amount from the file?

  2. #2
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Change a double value

    Show us what you've got and where you're getting errors/unexpected results.

  3. #3
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Change a double value

    I am not getting any results yet, because I am not sure how to do it...

    Java Code:
    Double value1 = -150.00;
    If this value is negative, like this example, I would like to make a new value like this:

    Java Code:
    Double value2 = 150.00;
    And vice versa....

  4. #4
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Change a double value

    Java Code:
    Your value * -1
    will switch it from negative to positive and vice versa.

  5. #5
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Change a double value

    Java Code:
    String Amount   = textAmount.getText();
    Amount = Amount.replace(" ", "");
    Amount = Amount.replace(",", ".");
    double amount;
            if (!"".equals(Amount))
            {
                amount = Double.parseDouble(Amount);
            }else{
                amount = 0;
            }
            
    double amount2 = * -amount;
    But I get an error:
    Java Code:
    illegal start of expression

  6. #6
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Change a double value

    The *(asterisk) has to be in front of the =(equals)

    Java Code:
    // +150.00
    double amount1 = 150.0;
    //-150.00
    double amount2 = amount1 * -1;

  7. #7
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Change a double value

    Java Code:
    String Amount   = textAmount.getText();
    Amount = Amount.replace(" ", "");
    Amount = Amount.replace(",", ".");
    double amount;
    if (!"".equals(Amount))
            {
                amount = Double.parseDouble(Amount);
            }else{
                amount = 0;
            }
            
    double amount2 = amount * -1;
    Work perfect. Thanks JBelg!!

Similar Threads

  1. Replies: 2
    Last Post: 09-25-2012, 05:53 PM
  2. Replies: 1
    Last Post: 01-03-2012, 12:16 AM
  3. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 11:40 AM
  4. Replies: 6
    Last Post: 03-17-2009, 03:23 AM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •