DoubleUtlTest.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. XOWA: the XOWA Offline Wiki Application
  3. Copyright (C) 2012-2021 gnosygnu@gmail.com
  4. XOWA is licensed under the terms of the General Public License (GPL) Version 3,
  5. or alternatively under the terms of the Apache License Version 2.0.
  6. You may use XOWA according to either of these licenses as is most appropriate
  7. for your project on a case-by-case basis.
  8. The terms of each license can be found in the source code repository:
  9. GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
  10. Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
  11. */
  12. package gplx.objects.primitives;
  13. import gplx.frameworks.tests.GfoTstr;
  14. import gplx.types.basics.utls.DoubleUtl;
  15. import org.junit.Test;
  16. public class DoubleUtlTest {
  17. private final DoubleUtlTstr tstr = new DoubleUtlTstr();
  18. @Test
  19. public void TrimZeroes() {
  20. tstr.TestTrimZeroes("12.100" , "12.1");
  21. tstr.TestTrimZeroes("12.000" , "12");
  22. tstr.TestTrimZeroes("12.001" , "12.001");
  23. tstr.TestTrimZeroes("1020.00" , "1020");
  24. tstr.TestTrimZeroes("1020.00" , "1020");
  25. tstr.TestTrimZeroes("1.200e5" , "1.2E5");
  26. tstr.TestTrimZeroes("1.200e-05" , "1.2E-5");
  27. }
  28. @Test
  29. public void ToStrByPrintF() {
  30. tstr.TestToStrByPrintF(1d / 2d , "0.5"); // fails with 0.50000000000000
  31. tstr.TestToStrByPrintF(5d / 100000000000000000d, "5E-17"); // fails with 5.0000000000000e-17
  32. tstr.TestToStrByPrintF(7538000d / 7773352000d , "0.00096972322879499"); // fails with 0; ISSUE#:697; DATE:2020-08-11
  33. tstr.TestToStrByPrintF(56225d / 7776747000d , "7.2298867379895E-06"); // fails with 0; ISSUE#:697; DATE:2020-08-11
  34. tstr.TestToStrByPrintF(35746d / 7805411000d , "4.5796435319037E-06"); // fails with 0; ISSUE#:697; DATE:2020-08-11
  35. }
  36. @Test public void Xto_str_loose() {
  37. tstr.TestXtoStrLoose(2449.6000000d , "2449.6");
  38. tstr.TestXtoStrLoose(623.700d , "623.7");
  39. }
  40. }
  41. class DoubleUtlTstr {
  42. public void TestToStrByPrintF(double v, String expd) {GfoTstr.Eq(expd, DoubleUtl.ToStrByPrintF(v));}
  43. public void TestTrimZeroes(String val, String expd) {GfoTstr.Eq(expd, DoubleUtl.TrimZeroes(val));}
  44. public void TestXtoStrLoose(double v, String expd) {GfoTstr.Eq(expd, DoubleUtl.ToStrLoose(v));}
  45. }