Get the Decimal Part from a Double

Getting the decimal part of a double in Swift

Without converting it to a string, you can round up to a number of decimal places like this:

let x:Double = 1234.5678
let numberOfPlaces:Double = 4.0
let powerOfTen:Double = pow(10.0, numberOfPlaces)
let targetedDecimalPlaces:Double = round((x % 1.0) * powerOfTen) / powerOfTen

Your output would be

0.5678

How to get numbers after decimal point?

An easy approach for you:

number_dec = str(number-int(number))[1:]

Get decimal portion of a number with JavaScript

Use 1, not 2.

js> 2.3 % 1
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.

How to get the decimal part of a float?

float only has a few digit of precision so you should expect to see a round error fairly easily. try double this has more accuracy but still has rounding errors. You have to round any answer you get to have a sane output.

If this is not desireable you can use BigDecimal which does not have rounding errors, but has its own headaches IMHO.

EDIT: You may find this interesting. The default Float.toString() uses minimal rounding, but often its not enough.

System.out.println("With no rounding");
float n = 22.65f;
System.out.println("n= "+new BigDecimal(n));
float expected = 0.65f;
System.out.println("expected= "+new BigDecimal(expected));

System.out.println("n % 1= "+new BigDecimal(n % 1));
System.out.println("n - Math.floor(n) = "+new BigDecimal(n - Math.floor(n)));
System.out.println("n - (int)n= "+new BigDecimal(n - (int)n));

System.out.println("With rounding");
System.out.printf("n %% 1= %.2f%n", n % 1);
System.out.printf("n - Math.floor(n) = %.2f%n", n - Math.floor(n));
System.out.printf("n - (int)n= %.2f%n", n - (int)n);

Prints

With no rounding
n= 22.6499996185302734375
expected= 0.64999997615814208984375
n % 1= 0.6499996185302734375
n - Math.floor(n) = 0.6499996185302734375
n - (int)n= 0.6499996185302734375
With rounding
n % 1= 0.65
n - Math.floor(n) = 0.65
n - (int)n= 0.65

Get value after decimal point from double variable

Divide the double by 1 using truncatingRemainder(dividingBy:) and get the reminder.

var doubleVar = 234.045
var new = doubleVar.truncatingRemainder(dividingBy: 1.0)
let rounded = Double(round(1000*new)/1000)
print(rounded)

OR

Using C function modf

var doubleVar = 234.045
let splitPi = modf(doubleVar)
splitPi.0 // 324.0
splitPi.1 // 0.045


Related Topics



Leave a reply



Submit