Back to Repositories

Testing Number Formatting and Precision Handling in AndroidUtilCode

This test suite validates the NumberUtils class functionality in AndroidUtilCode, focusing on number formatting and float-to-double conversion operations. The tests ensure precise decimal handling and number format customization capabilities.

Test Coverage Overview

The test suite provides comprehensive coverage of number formatting operations and type conversions.

  • Decimal place precision testing with various configurations
  • Number formatting with padding and thousand separators
  • Float to double conversion accuracy verification
  • Edge cases for rounding behavior

Implementation Analysis

The testing approach utilizes JUnit’s assertion framework to validate number formatting accuracy and precision. The implementation demonstrates systematic testing of method overloads with different parameter combinations.

  • Parameterized testing of format() method variations
  • Precision control validation
  • Rounding behavior verification

Technical Details

  • Testing Framework: JUnit 4
  • Test Environment: Java
  • Key Methods: format(), float2Double()
  • Assert Methods: assertEquals()
  • Test Data: Math.PI multiplication for precision testing

Best Practices Demonstrated

The test suite exhibits strong testing practices through methodical validation of number formatting scenarios.

  • Comprehensive method overload testing
  • Systematic precision validation
  • Clear test case organization
  • Effective use of constants and test data

blankj/androidutilcode

lib/utilcode/src/test/java/com/blankj/utilcode/util/NumberUtilsTest.java

            
package com.blankj.utilcode.util;

import org.junit.Assert;
import org.junit.Test;

/**
 * <pre>
 *     author: blankj
 *     blog  : http://blankj.com
 *     time  : 2020/04/13
 *     desc  : test NumberUtils
 * </pre>
 */
public class NumberUtilsTest {

    @Test
    public void format() {
        double val = Math.PI * 100000;// 314159.2653589793

        Assert.assertEquals("314159.27", NumberUtils.format(val, 2));
        Assert.assertEquals("314159.265", NumberUtils.format(val, 3));

        Assert.assertEquals("314159.27", NumberUtils.format(val, 2, true));
        Assert.assertEquals("314159.26", NumberUtils.format(val, 2, false));

        Assert.assertEquals("00314159.27", NumberUtils.format(val, 8, 2, true));
        Assert.assertEquals("0000314159.27", NumberUtils.format(val, 10, 2, true));

        Assert.assertEquals("314,159.27", NumberUtils.format(val, true, 2));
        Assert.assertEquals("314159.27", NumberUtils.format(val, false, 2));

        Assert.assertEquals("314159.27", NumberUtils.format(val, false, 2, true));
        Assert.assertEquals("314159.26", NumberUtils.format(val, false, 2, false));

        Assert.assertEquals("314159.27", NumberUtils.format(val, false, 2, true));
        Assert.assertEquals("314159.265", NumberUtils.format(val, false, 3, false));
    }

    @Test
    public void float2Double() {
        float val = 3.14f;
        System.out.println((double) val);
        System.out.println(NumberUtils.float2Double(val));
    }
}