-2

For a project at school, I have to make a java program to retrieve data from a database.

This is my code:

import java.text.*;
import java.util.*;
public class Tijdstip
{
    public Tijdstip()
    {
    }
    public double testTijd(String tijdstip1)
    {
        // splitting the time
        String[] tokens = tijdstip1.split("\\s+");
        int hours = Integer.parseInt(tokens[0]);
        int minutes = Integer.parseInt(tokens[1]);  
        //returning the time
        double result = hours + ((double)minutes/100); 
        return result;      
    }
}

I fill in a time as string like: "7 10", meaning 7:10am and it must return a double like 7.10 But it returns 7.1, how do I make it so it will return 7.10 instead of 7.1?

rebeliagamer
  • 1,218
  • 16
  • 32
Denny
  • 1,586
  • 3
  • 15
  • 32
  • 4
    7 hours and 10 minutes is 7.1666... hours, not 7.1 hours.... – Boann Oct 17 '13 at 11:16
  • 4
    Do you really want to store time in a double? – Kevin DiTraglia Oct 17 '13 at 11:17
  • @Boann it doesnt matter, i just want it to show 7.10 – Denny Oct 17 '13 at 11:18
  • @KevinDiTraglia yes it has to be a double – Denny Oct 17 '13 at 11:18
  • It doesn't actually return 7.1, but a double that happens to be easily displayed by default as 7.1. Use Maroun's solution, or e.g `String.format("%.2f",result);` (doc [here](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#format%28java.lang.String,%20java.lang.Object...%29)) if you don't mind a light C taste in your Java. – fvu Oct 17 '13 at 11:21
  • possible duplicate of [How to round a number to n decimal places in Java](http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) – user207421 Oct 17 '13 at 11:28
  • Why do you ever want to hold this as a double (using the decimal portion for minutes rather than fractions of an hour: 7.59+0.01=8.00 is madness). Why not go straight from your string to some form of time object? – Richard Tingle Oct 17 '13 at 11:32
  • It just has to be this way – Denny Oct 17 '13 at 11:34
  • Fair enough, sometimes there are just weird requirements, as long as you know its madness – Richard Tingle Oct 17 '13 at 11:35
  • 1
    I know i'm pushing this, but what is the requirement reventing you returning this as your own object, lets call it `Time` that holds `hours` and `minutes` sperately, with a constructor like `Time time=new Time(hours, minutes)` and then you can call `time.hours`, or `time.minutes` as you saw fit – Richard Tingle Oct 17 '13 at 11:42
  • 1
    @RichardTingle Or even returning time as a plain `int` number of minutes, then displaying it as `System.out.println(String.format("%d:%02d", minutes / 60, minutes % 60));` It will make for easy time arithmetic. – Boann Oct 17 '13 at 11:50
  • R.E. your Gui board comment on the now deleted answer: @Denny ok, but when it goes on the Gui board it is a String, format it at that point, send a string to the Gui board – Richard Tingle Oct 17 '13 at 11:50
  • @Boann Actually very sensible, and simple. You don't even have to worry about hour changes that way – Richard Tingle Oct 17 '13 at 11:51
  • @RichardTingle No, I have to send a double to it to make it work like I want – Denny Oct 17 '13 at 11:52
  • @Denny Then it is impossible. A double is a number, you are not treating it like a number and java understandably will not accomadate that; nor will any language – Richard Tingle Oct 17 '13 at 11:53
  • 1
    Similarly people often treat phone "numbers" as numbers, but they're not numbers, you don't add them together or divide them. Phone numbers are "words" written entirely in digits. The number of hours is a number, but 7:10 is not a number, it is a "time" – Richard Tingle Oct 17 '13 at 11:57
  • But is there any way to return 0.1 as 0.10? – Denny Oct 17 '13 at 12:00
  • No, no there is not, for the reasons I previously stated, the number 0.1 **is** the number 0.10 and 0.100, it returns all of them at the same time – Richard Tingle Oct 17 '13 at 12:01
  • 1
    Okay it's clear. My question has been answered – Denny Oct 17 '13 at 12:03
  • 1
    "I have to send a double to it to make it work like I want". It won't work for arithmetic either if that's your idea. E.g., 7:10 + 8:40 + 9:50 is 25:40, but because you divide by 100 instead of 60 you'll get 25.0. – Boann Oct 17 '13 at 12:38

5 Answers5

2

Try this

new DecimalFormat("#.00").format(result);
Suresh Atta
  • 114,879
  • 36
  • 179
  • 284
Ankur
  • 120
  • 2
2

You need to understand the difference between how a number is represented and how it is displayed. There is no numeric difference between 7.1 and 7.10; there is no way to make the number one instead of the other. You can display 7.1 as 7.10 using output formatting such as found in the Format class.

As a side issue: Storing this as a double would be a bad idea in a program of any size. There are many classes for representing time, and they all take into account the non-decimal nature of time divisions. Doubles don't do this.

arcy
  • 11,973
  • 8
  • 54
  • 89
  • 1
    Is there really no way to return it as 7.10? – Denny Oct 17 '13 at 11:35
  • 1
    @Denny Its a number, not a string, 7.1==7.10==7.100=7.1000==7.10000, it returns **all** of them, at the same time – Richard Tingle Oct 17 '13 at 11:37
  • 1
    @Denny Whether you initialize a double with 7.10 or 7.1, the internal memory bits are identical. It is impossible for a double variable to remember the difference. You can see the internal zeros and ones by doing `System.out.println(Long.toBinaryString(Double.doubleToLongBits(7.1)));` and `System.out.println(Long.toBinaryString(Double.doubleToLongBits(7.10)));`. You'll see they're the same. – Boann Oct 17 '13 at 11:39
1

The short answer is that you cannot do this if you must keep the result as a double. The double doesn't know anything about leading or trailing zeros. You can only do this when the result is formatted as a String. E.g., String.format("%.2f", 7.1) gives the string "7.10". You can easily do this formatting every time you display the number, but you cannot make the number itself remember the extra zero.

Boann
  • 44,932
  • 13
  • 106
  • 138
0

When you return double it will always truncate your last number if it is zero. so make it a string and return

  • No it won't. Answer is basically nonsense. – user207421 Oct 17 '13 at 11:26
  • @Boann It can't be correct if it is meaningless. What does 'truncate the last number if it is zero' mean? If it means 'truncate the last zero digit' it is certainly not correct. – user207421 Nov 09 '13 at 00:48
-1

If you need double value which has only two digits after dicimal, you can try following:

double d = 1.164444;       
double r = Math.rint(d * 100)/100;
System.out.println(r);
Ashwinee K Jha
  • 8,648
  • 2
  • 21
  • 18