Results 1 to 9 of 9
  1. #1
    Argyros is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Arithmetic expression that will display doubles to only one decimal place?

    I am looking to find a way to display the number of decimal places without using any type of formatting string.
    Only to make it xxx.x instead of xxx.xxxxxx.
    Last edited by Argyros; 09-29-2014 at 01:41 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Arithmetic expression that will display doubles to only one decimal place?

    So you want to format a number without describing how to format a number. You can't do it unless you write your own
    format code. I suggest you read about System.out.printf().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Arithmetic expression that will display doubles to only one decimal place?

    Quote Originally Posted by Argyros View Post
    I am looking to find a way to display the number of decimal places without using any type of formatting string.
    Only to make it xxx.x instead of xxx.xxxxxx.
    With or without a computer?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Arithmetic expression that will display doubles to only one decimal place?

    Quote Originally Posted by DarrylBurke View Post
    With or without a computer?
    With; a wooden one please.

    kindest regards,

    Jos ;-)
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Arithmetic expression that will display doubles to only one decimal place?

    Quote Originally Posted by JosAH View Post
    With; a wooden one please.

    kindest regards,

    Jos ;-)
    The nearest I have to wood is one made from spiky green cacti that only works when you sit on it. Will that do?

    db --- prepares --- to --- (oh you know the rest) :
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Arithmetic expression that will display doubles to only one decimal place?

    Quote Originally Posted by DarrylBurke View Post
    The nearest I have to wood is one made from spiky green cacti that only works when you sit on it. Will that do?
    No, our society that propagates "how not to sit on hedgehogs" have recently included cacti in our field of interest.

    kindest regards,

    Jos (<--- never sits on hedgehogs nor cacti (nor jellyfish for that matter))
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    Argyros is offline Member
    Join Date
    Aug 2014
    Posts
    10
    Rep Power
    0

    Default Re: Arithmetic expression that will display doubles to only one decimal place?

    Here is how I was told to do it.

    With a little math manipulation, the number of decimal places can be formatted.
    Try to write an arithmetic expression that will display doubles to only one
    decimal place. (Hint: Write a decimal number with four or five decimal places on
    a piece of paper. What kind of arithmetic will move the decimal point around
    within the number? Don’t forget about casting, too.)

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Arithmetic expression that will display doubles to only one decimal place?

    This sounds like an exercise for you to develop your own formatting method. There is not
    much more I can say than what was given. It is something you sort of have to see by playing
    around with. I would first suggest you try to just convert a number like 23.398 to 23.3.
    Then when you get that done, you can try to properly round it up. Doing this on paper first
    is the best bet.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Arithmetic expression that will display doubles to only one decimal place?

    And then we encounter ncb-goldberg ...

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Arithmetic expression calculator
    By CGHMN in forum New To Java
    Replies: 3
    Last Post: 07-11-2011, 04:28 PM
  2. Find the value of the arithmetic expression...
    By hydride in forum Advanced Java
    Replies: 3
    Last Post: 03-16-2011, 09:17 AM
  3. Replies: 0
    Last Post: 02-06-2011, 09:29 PM
  4. Arithmetic using doubles
    By Black_Eye in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 08:35 AM
  5. arithmetic with doubles problem
    By hoosierfan24 in forum Java Software
    Replies: 1
    Last Post: 10-04-2010, 03:11 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
  •