Force point (“.”) as decimal separator in java

Use the overload of String.format which lets you specify the locale: return String.format(Locale.ROOT, “%.2f”, someDouble); If you’re only formatting a number – as you are here – then using NumberFormat would probably be more appropriate. But if you need the rest of the formatting capabilities of String.format, this should work fine.

Double vs Decimal Rounding in C#

Why 1/3 as a double is 0.33333333333333331 The closest way to represent 1/3 in binary is like this: 0.0101010101… That’s the same as the series 1/4 + (1/4)^2 + (1/4)^3 + (1/4)^4… Of course, this is limited by the number of bits you can store in a double. A double is 64 bits, but one … Read more

Modulus with doubles in Java

Use a precise type if you need a precise result: double val = 3.9 – (3.9 % 0.1); System.out.println(val); // 3.8000000000000003 BigDecimal x = new BigDecimal( “3.9” ); BigDecimal bdVal = x.subtract( x.remainder( new BigDecimal( “0.1” ) ) ); System.out.println(bdVal); // 3.9 Why 3.8000…003? Because Java uses the FPU to calculate the result. 3.9 is … Read more

Dividing two integers to a double in java

This line here d = w[L] /v[L]; takes place over several steps d = (int)w[L] / (int)v[L] d=(int)(w[L]/v[L]) //the integer result is calculated d=(double)(int)(w[L]/v[L]) //the integer result is cast to double In other words the precision is already gone before you cast to double, you need to cast to double first, so d = ((double)w[L]) … Read more