Created
September 27, 2017 19:43
-
-
Save garrypolley/c3d2dd2720f455cdc23d0349bc28a770 to your computer and use it in GitHub Desktop.
A test of using BigNum and Floats. Conclusion: when you store a float in the database it seems to be about the same if you are careful about how you use the floats.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigDecimal; | |
import java.math.MathContext; | |
import java.math.RoundingMode; | |
import java.util.Scanner; | |
import java.lang.Float; | |
class BigNum { | |
public static void main(String[] args) { | |
System.out.println("Hi"); | |
BigDecimal jim = new BigDecimal("3.2"); | |
BigDecimal billJim = jim.add(new BigDecimal("3.2")); | |
System.out.println(String.format("Big Decimal The jim: %.10f", jim)); | |
System.out.println(String.format("Big Decimal the billJim: %.10f", billJim)); | |
float fjim = new Float("3.2"); | |
float fBillJim = fjim + fjim; | |
System.out.println(String.format("Float The fjim: %.10f", fjim)); | |
System.out.println(String.format("Float the fbillJim: %.10f", fBillJim)); | |
System.out.println("All to float after rounding: "); | |
jim.round(new MathContext(2, RoundingMode.HALF_EVEN)); | |
billJim.round(new MathContext(2, RoundingMode.HALF_EVEN)); | |
System.out.println(String.format("Big Decimal The jim: %.10f", jim.floatValue())); | |
System.out.println(String.format("Big Decimal the billJim: %.10f", billJim.floatValue())); | |
float djim = Math.round(fjim * 100) / 100; | |
float dbillJim = Math.round(fBillJim * 100) / 100; | |
System.out.println(String.format("Float The fjim: %.10f", djim)); | |
System.out.println(String.format("Float the fbillJim: %.10f", dbillJim)); | |
} | |
} | |
/* | |
OUTPUT: | |
Hi | |
Big Decimal The jim: 3.2000000000 | |
Big Decimal the billJim: 6.4000000000 | |
Float The fjim: 3.2000000477 | |
Float the fbillJim: 6.4000000954 | |
All to float after rounding: | |
Big Decimal The jim: 3.2000000477 | |
Big Decimal the billJim: 6.4000000954 | |
Float The fjim: 3.0000000000 | |
Float the fbillJim: 6.0000000000 | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment