Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9221433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:34:42+00:00 2026-06-18T03:34:42+00:00

public static double centsToDollars(Number cents, int precision) { return BigDecimal.valueOf(cents.doubleValue() / 100).setScale(precision, RoundingMode.DOWN).doubleValue(); }

  • 0
public static double centsToDollars(Number cents, int precision) {
    return BigDecimal.valueOf(cents.doubleValue() / 100).setScale(precision, RoundingMode.DOWN).doubleValue();
}

Code above works completely fine when I want to display cents value in dollars. For example, for 1 cent, it returns 0.01 dollar.

assertEquals("$0.01", FormatUtils.centsToDollars(1, 3)) 
assertEquals("$0.012", FormatUtils.centsToDollars(1.2345, 3))
assertEquals("$0.327", FormatUtils.centsToDollars(32.7, 3))

But I can’t figure out, why FormatUtils.centsToDollars(0.65, 3) returns $0.0060. I expect to receive 0.006 instead. What is the latest zero about ?

Update

Looks like the root cause of the issue is invocation of doubleValue() of BigDecimal

System.out.println(Double.parseDouble("0.006")); 

System.out.println(BigDecimal.valueOf(0.006).doubleValue());

returns 0.0060 for me

Any clue why this happens ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:34:44+00:00Added an answer on June 18, 2026 at 3:34 am

    parseDouble of Double class adds extra zero

    There is a bug id:4428022 in Java 1.4 to 6 which means it adds an extra zero you don’t need. This happens for values 0.001 to 0.009 only. Java 7 doesn’t have this bug.

    for (int i = 1; i <= 9; i++)
        System.out.println(i / 1000.0);
    

    in Java 6 prints

    0.0010
    0.0020
    0.0030
    0.0040
    0.0050
    0.0060
    0.0070
    0.0080
    0.0090

    but in Java 7 prints

    0.001
    0.002
    0.003
    0.004
    0.005
    0.006
    0.007
    0.008
    0.009


    I suspect that 0.65 is actually slightly less in reality. When you divide it by 100 you get something like 0.006499999999999 which when rounded drops to 0.006

    I suspect what you wanted was

    public static String centsToDollars(Number cents, int precision) {
        return "$" + BigDecimal.valueOf(cents.doubleValue())
               .divide(BigDecimal.valueOf(100))
               .setScale(precision, RoundingMode.HALF_UP);
    }
    

    Try

    System.out.println(new BigDecimal(0.65 / 100));
    

    This is how I would write it

    public static String centsToDollars(double cents) {
        double rounded = Math.round(cents * 100) / 1e4;
        return "$" + rounded;
    }
    

    This assumes two decimal places of cents.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static string RatingCalculator(int input) { if (input < 10) { return string.Empty; }
int main() { class A { public: static double test_code(const A& a); }; class
I have two functions public static double avg(int[] values) { if(values == null ||
import java.util.*; class Averager { public static double unlimited() { int count = 0;
I've implemented code that should return present location. First the code: public static double[]
This code have something wrong, having error in return type. public static []double convertLatLong(String
public static void main(String args []) { Scanner in = new Scanner(System.in); int number
public class ExperienceTable { public static int MaxExperiencePoints; public static double PercentToNextLevel; public static
In OpenJDK, for the method: public static Double valueOf(double d) The javadoc says: Returns
What does the following function perform? public static double CleanAngle(double angle) { while (angle

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.