Results 1 to 7 of 7

Thread: Java BigDecimal to Integer

  1. #1
    Join Date
    Mar 2010
    Posts
    383

    Java BigDecimal to Integer

    Hello,
    I have a question about a number BigDecimal. The number is 5483.238 for example (I know it's small for a BigDecimal, but it is a constraint imposed on me) and I need the form of two integers. The first integer is the entire part number 5483. The second integer is the decimal part of number two digits 24. I arrived safely to get the first integer using BigDecimal.intValue (), for cons I turn round to the second. Can you help me with this? Thank you for your help.

  2. #2
    Join Date
    Mar 2010
    Posts
    197

    Re: Java BigDecimal to Integer

    The following code may help you

    Code:
    ValueBD bgdc = new bgdc("-124.9365");
    int er = vlbd.intValue(); bgdc decimalPartBD = vlbd.subtract(bgdc.valueOf(er)).setScale(2, bgdc.ROUND_UP);
    int percent = bgdc.valueOf(100).multiply(decimalPartBD).intValue();

  3. #3
    Join Date
    Mar 2010
    Posts
    162

    Re: Java BigDecimal to Integer

    Yes, it works I think, the question might be: What makes you serve there? I do not see why you transform the entire BigDecimal, since normally, all manipulations are useful for decimal numbers correctly included in the BigDecimal.

  4. #4
    Join Date
    Mar 2010
    Posts
    383

    Re: Java BigDecimal to Integer

    Thank you for your reply, actually I'm using this for a very specific application. The aim is to bring you value the number to read through audio files, so I'm breaking my value 1000.wav example, 900.wav, virgule.wav 24.wav, 99.wav to read 1924.99. This is the reason why I chose to treat the two parties as the number of integers.

  5. #5
    Join Date
    Dec 2009
    Posts
    296

    Re: Java BigDecimal to Integer

    Here is another code that might help you in conversion
    Code:
    temp BigDecimal = new BigDecimal(tot).setScale(2, BigDecimal.ROUND_HALF_EVEN); BigDecimal prs = new BigDecimal("100").setScale(2, BigDecimal.ROUND_HALF_EVEN);
    if(temp.equals(one hundred))
            System.out.System.out.println("equal");
    else
            System.out.System.out.println("different");

  6. #6
    Join Date
    Mar 2010
    Posts
    330

    Re: Java BigDecimal to Integer

    Unfortunately, the equals method compares the scale of the BigDecimal. But you can use CompareTo, which, before comparing the two objects is the same scale (the larger of two). What you could do, too, it's either you make a personal method somewhere to compare BigDecimal or form a class by overloading equals BigDecimal so it does not compare the scale. (Do not forget to also override hashCode in this case, since it also uses the scale)

  7. #7
    Join Date
    Mar 2010
    Posts
    330

    Re: Java BigDecimal to Integer

    Simply by return compareTo (...) == 0; equals in (...). That is odd that they have done that in the JDK (a mistake I think), because it is recommended that for every Similar, property. CompareTo (...) == 0 ==>. Equals (. ..) is true.

Similar Threads

  1. BigDecimal and Date
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 07-02-2010, 02:21 AM
  2. How to know the length of an integer in C++
    By troop in forum Software Development
    Replies: 4
    Last Post: 29-01-2010, 09:32 PM
  3. conversion of Object into integer in JAVA
    By Jaisudha in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 12:45 PM
  4. Replies: 4
    Last Post: 25-09-2009, 05:09 PM
  5. Verillo's integer and reg?
    By Fason in forum Networking & Security
    Replies: 3
    Last Post: 25-10-2008, 01:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,204,735.86913 seconds with 17 queries