Rounding up decimals in java

Discussion in 'Software' started by slashgonewylde, Oct 8, 2009.

  1. slashgonewylde

    slashgonewylde Private E-2

    Making a program that envolves money. Even using DecimalFormat you still get decimals to over two places. I am wondering how I can get it to round up to two places.

    Thanks,
    Matt
     
  2. MutD

    MutD Specialist

    Code:
    DecimalFormat decFormat = new DecimalFormat( "#,###,###,##0.00" );
    double myDouble = 100.2397;
    double myRoundedDouble = new Double(decFormat.format(myDouble)).doubleValue();
    
    The value of myRoundedDoublewill be 100.24.


    Or, if you dont want to use Decimal Format class:

    Code:
    x = (double)int((myDouble+0.005)*100.0)/100.0;
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds