Results 1 to 5 of 5
  1. #1
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default forming negative numbers

    Hi,

    What would be the best way to form negative numbers from a string "-" and a number?

    I can convert the number to a string, so I'd have say "-5", but that's a string and not really the number -5.

    Is there a way to tell Java that it's a number with a negative value?

    Thanks!

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    But why do you want to tell java that its a negative number.

    For jvm a number is a number, no matter positive or negative. jvm is not bothered till the time number is valid and within range.

    It would be your program, which needs to differentiate between positive and negative numbers. A simple check of lessThan and greaterThan Zero(0) can be used to differentiate between numbers.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    I am not sure, but are you looking for
    int i = Integer.parseInt("-5")
    :confused:

  4. #4
    TopNFalvors is offline Member
    Join Date
    Mar 2011
    Posts
    41
    Rep Power
    0

    Default

    Oh ok, so if I had a string "-5" and used parseInt, it would turn it into an integer of -5?

    Thanks

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by TopNFalvors View Post
    Oh ok, so if I had a string "-5" and used parseInt, it would turn it into an integer of -5?

    Thanks
    Hm what's the quickest way to find this out?

Similar Threads

  1. Mod of Negative Numbers
    By Venny in forum New To Java
    Replies: 7
    Last Post: 01-28-2011, 06:32 AM
  2. Help with negative numbers
    By cela0811 in forum New To Java
    Replies: 2
    Last Post: 01-04-2011, 06:04 PM
  3. Byte value comes out negative
    By Bluefox815 in forum New To Java
    Replies: 1
    Last Post: 07-03-2010, 04:52 AM
  4. I don't want negative money [BUG]
    By anthonym2121 in forum New To Java
    Replies: 1
    Last Post: 04-07-2009, 09:06 AM
  5. Negative elements
    By swikar.java in forum New To Java
    Replies: 6
    Last Post: 12-15-2008, 05:48 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
  •