Minichan

Topic: Is there a way to split a double into an integer and a double in Java?

Catherine !ttGirlsPl2 started this discussion 7 years ago #84,794

Like take a number like 2.45 and turn it into 2 and 0.45?

I consulted Google but none of the things people proposed works on repl.it. I only get an error message saying "lossy attempt at conversion."

Anonymous B joined in and replied with this 7 years ago, 23 minutes later[^] [v] #973,237

From a Stack Overflow thread.

double value = 2.45;
double fraction=value%1; //remainder from mod%1 division leave everything after the decimal point
int integer=(int)value; //cast as int to keep everything before the decimal point

Catherine !ttGirlsPl2 (OP) replied with this 7 years ago, 21 minutes later, 44 minutes after the original post[^] [v] #973,244

@previous (B)
> Main.java:11: error: incompatible types: possible lossy conversion from double to int
Does repl.it not like this?

Anonymous B replied with this 7 years ago, 17 minutes later, 1 hour after the original post[^] [v] #973,251

@previous (Catherine !ttGirlsPl2)
Oh, it's complaining about the cast. I'm not up on what Java complains about lately. Persnickety little machine. You should slap it around and tell it do what you say. Walk the cast through a float? Use Math.round()? I don't know. The integer bit should be the easiest part.

Anonymous C joined in and replied with this 7 years ago, 57 seconds later, 1 hour after the original post[^] [v] #973,253

@973,244 (Catherine !ttGirlsPl2)
Do your own damned homework for your on line junior college math glass!

Anonymous D joined in and replied with this 7 years ago, 3 minutes later, 1 hour after the original post[^] [v] #973,256

@previous (C)

> Do your own damned homework for your on line junior college math glass!

Anonymous B replied with this 7 years ago, 55 seconds later, 1 hour after the original post[^] [v] #973,257

It looks like Math.round() returns a long. So you might still have to cast it as int like: int integer = (int)Math.round(value);

Jesus, what a clusterfuck. You used to be able to just tell machines to do stupid things and they would do them no questions asked.

(Edited 2 minutes later.)

Anonymous E joined in and replied with this 7 years ago, 2 minutes later, 1 hour after the original post[^] [v] #973,258

@previous (B)
> You used to be able to just tell machines to do stupid things and they would do them no questions asked.

Guessing you came along just after the ages of Machine programs locked up in Do Loops.

Anonymous B replied with this 7 years ago, 4 minutes later, 1 hour after the original post[^] [v] #973,263

@previous (E)
It was a golden age when human stupidity still outmatched machine intelligence.

Catherine !ttGirlsPl2 (OP) replied with this 7 years ago, 2 hours later, 3 hours after the original post[^] [v] #973,339

I think I just made a program that can convert D°M'S'' into degrees and vice versa.

Catherine !ttGirlsPl2 (OP) double-posted this 7 years ago, 1 minute later, 3 hours after the original post[^] [v] #973,340

@973,257 (B)
I wish I knew how to round to x amount of decimal places.

Sheila LaBoof joined in and replied with this 7 years ago, 8 hours later, 12 hours after the original post[^] [v] #973,406

If it's not with a built-in function, then you could multiply the number by 10^x, then round, then divide by 10^x.
:

Please familiarise yourself with the rules and markup syntax before posting.