Removing decimal from double? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Removing decimal from double?

RojBlakeRojBlake Posts: 1Member

I've only just started learning Java, and I've just progressed from programs like "HelloWorld" and such

I've written an extremely simple and basic program, that measures time dilation when a fictive space vessle travels close to the speed of light...
It's working as I've intended, however, my ambition is to remove all decimals from the answer. For example if I get answer of 15.93848929393, I've used
Math.floor to round the double off, however now it will be displayed as 15.0 and I would like to be rid of the decimal completely.
Is there anybody who can give me a suggestion as to how to remove the decimal?
Please bear with me, I'm a complete noob at this, and there's a lot I still don't know and understand...

Here is the code:

import javax.swing.*;
import java.lang.Math;

public class TimeDilation {
public void dilation(){

double speedoflight, result, round;

speedoflight = Double.parseDouble(JOptionPane.showInputDialog("En ter lightspeed"));
result = 100*(Math.sqrt(1-(Math.pow(speedoflight, 2))));
round = Math.floor(result);

JOptionPane.showMessageDialog(null, "If you travel for 100 years on Earth " + round + " years will pass on The Magellan");



Thanks in advance for any replies!!


  • joet1984joet1984 Posts: 2Member
    Many ways to do it. You can use an integer instead of a double

    double speedoflight, round;
    int result;

    speedoflight = Double.parseDouble(JOptionPane.showInputDialog("En ter lightspeed"));
    result = 100*(Math.sqrt(1-(Math.pow(speedoflight, 2))));

    The difference between double and int is double has a decimal point value and int does not. (Also, the compiler sets aside more memory for a double so it can hold a larger value). If for some reason you needed the floor you could do

    int resultWithoutDecimal;

    resultWithoutDeciaml = (int) result;

    The (int) before the result means you are casting a double to an int. You can read up on that if you are interested
Sign In or Register to comment.