How to Get Whole and Fractional Parts from Double in Jsp/Java

How do I get whole and fractional parts from double in JSP/Java?

http://www.java2s.com/Code/Java/Data-Type/Obtainingtheintegerandfractionalparts.htm

double num;
long iPart;
double fPart;

// Get user input
num = 2.3d;
iPart = (long) num;
fPart = num - iPart;
System.out.println("Integer part = " + iPart);
System.out.println("Fractional part = " + fPart);

Outputs:

Integer part = 2
Fractional part = 0.2999999999999998

How to extract fractional digits of double/BigDecimal

double number = 12345.6789; // you have this
int decimal = (int) number; // you have 12345
double fractional = number - decimal // you have 0.6789

The problem here is that the fractional part is not written in memory as "0.6789", but may have certain "offsets", so to say. For example 0.6789 can be stored as 0.67889999291293929991.

I think your main concern here isn't getting the fractional part, but getting the fractional part with a certain precision.

If you'd like to get the exact values you assigned it to, you may want to consider this (altho, it's not a clean solution):

String doubleAsText = "12345.6789";
double number = Double.parseDouble(doubleAsText);
int decimal = Integer.parseInt(doubleAsText.split("\.")[0]);
int fractional = Integer.parseInt(doubleAsText.split("\.")[1]);

But, as I said, this is not the most efficient and cleanest solution.

Getting the fractional part of a double value in integer without losing precision

Assuming you want to get back a positive fraction even for negative values, I'd go with

(int)round(fabs(value - trunc(value)) * 1e4)

which should give you the expected result 1234.

If you do not round and just truncate the value

(int)(fabs(value - trunc(value)) * 1e4)

(which is essentially the same as your original code), you'll end up with the unexpected result 1233 as 1.1234 - 1.0 = 0.12339999999999995 in double precision.

Without using round(), you'll also get the expected result if you change the order of operations to

(int)(fabs(value * 1e4 - trunc(value) * 1e4))

If the integral part of value is large enough, floating-point inaccuracies will of course kick in again.

You can also use modf() instead of trunc() as David suggests, which is probably the best approach as far as floating point accuracy goes:

double dummy;
(int)round(fabs(modf(value, &dummy)) * 1e4)

Separating double into integer and decimal parts

You could do a String split(...). And then Integer parseInt(...) to get back the two integer components.

Separating double into integer and decimal parts in android studio

int a = Integer.parseInt(s_input);//this line caused error when s_input represents decimal.
double b = (10 * input - 10 * input)/10;//b always equal zero

to get whole and fractional parts of a double, you could try using split

                String s_input = Input.getText().toString();
if (s_input.contains(".")) {
String[] split = s_input.split("\\.");
String whole = split[0];
String fractional = split[1];
}

Get whole and fractional part of a double

So why not just subtracting?

A = 1.8345
BeforeComma = num2str(fix(A))
AfterComma = strrep(num2str(A - BeforeComma),['0.'],'')

A little more elegant would be to use regexp

A = 1.8345
splittedNumber = regexp(num2str(A),'\.','split')
[BeforeComma, AfterComma] = splittedNumber{:}

A =

1.8345

BeforeComma =

1

AfterComma =

8345

If you want doubles with a predefined number of digits, you can do:

A = 1.834512
digits = 4
B = round(A,digits)
splittedNumber = str2double(regexp(num2str(A),'\.','split'));
[BeforeComma, AfterComma] = deal(splittedNumber(1), splittedNumber(2))


Related Topics



Leave a reply



Submit