PHP code example of gryfoss / int-precision-helper

1. Go to this page and download the library: Download gryfoss/int-precision-helper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gryfoss / int-precision-helper example snippets


$normalized = IntPrecisionHelper::fromString("12.34"); // Returns: 1234
$normalized = IntPrecisionHelper::fromString("0.05");  // Returns: 5

$normalized = IntPrecisionHelper::fromFloat(12.34); // Returns: 1234
$normalized = IntPrecisionHelper::fromFloat(0.99);  // Returns: 99

// Float input
$normalized = IntPrecisionHelper::normalize(12.34);   // Returns: 1234

// String input
$normalized = IntPrecisionHelper::normalize("12.34"); // Returns: 1234

// Integer input (multiplied by precision factor)
$normalized = IntPrecisionHelper::normalize(12);      // Returns: 1200

$float = IntPrecisionHelper::denormalize(1234); // Returns: 12.34
$float = IntPrecisionHelper::denormalize(99);   // Returns: 0.99

$display = IntPrecisionHelper::toView(1234);    // Returns: "12.34"
$display = IntPrecisionHelper::toView(1234, 3); // Returns: "1.234"

$float = IntPrecisionHelper::toFloat(1234); // Returns: 12.34

// 12.34 * 2.00 = 24.68
$result = IntPrecisionHelper::normMul(1234, 200); // Returns: 2468

// Multiple values: 12.34 * 2.00 * 1.50
$result = IntPrecisionHelper::normMul(1234, 200, 150); // Returns: 3702

// 12.34 / 2.00 = 6.17
$result = IntPrecisionHelper::normDiv(1234, 200); // Returns: 617

// 12.34 + 5.67 + 8.90 = 26.91
$result = IntPrecisionHelper::normAdd(1234, 567, 890); // Returns: 2691

// 12.34 - 5.67 = 6.67
$result = IntPrecisionHelper::normSub(1234, 567); // Returns: 667

// 50 out of 200 = 25.00%
$percentage = IntPrecisionHelper::calculatePercentage(50, 200); // Returns: 2500
$display = IntPrecisionHelper::toView($percentage); // Returns: "25.00"

$comparison = IntPrecisionHelper::normCompare(1234, 567); // Returns: 1

$isValid = IntPrecisionHelper::isValid(1234);    // Returns: true
$isValid = IntPrecisionHelper::isValid("1234");  // Returns: false
$isValid = IntPrecisionHelper::isValid(12.34);   // Returns: false

use GryfOSS\Formatter\IntPrecisionHelper;

// String to normalized integer
$normalized = IntPrecisionHelper::fromString("12.34"); // 1234

// Float to normalized integer
$normalized = IntPrecisionHelper::fromFloat(12.34); // 1234

// Universal normalize method (auto-detects type)
$normalized = IntPrecisionHelper::normalize(12.34);    // 1234 (float)
$normalized = IntPrecisionHelper::normalize("12.34");  // 1234 (string)
$normalized = IntPrecisionHelper::normalize(12);       // 1200 (integer)

// Back to string representation
$display = IntPrecisionHelper::toView(1234); // "12.34"

// Back to float
$float = IntPrecisionHelper::toFloat(1234); // 12.34

// Using denormalize (alias for toFloat)
$float = IntPrecisionHelper::denormalize(1234); // 12.34

// Demonstrate precision preservation
$originalFloat = 12.34;
$normalized = IntPrecisionHelper::normalize($originalFloat);
$restored = IntPrecisionHelper::denormalize($normalized);
// $restored === 12.34 (exact match)

$originalString = "99.99";
$normalized = IntPrecisionHelper::normalize($originalString);
$restored = IntPrecisionHelper::denormalize($normalized);
// $restored === 99.99 (exact match)

$originalInt = 100;
$normalized = IntPrecisionHelper::normalize($originalInt);
$restored = IntPrecisionHelper::denormalize($normalized);
// $restored === 100.0 (converted to float)

// Multiplication: 12.34 * 2.00 = 24.68
$result = IntPrecisionHelper::normMul(1234, 200); // 2468

// Division: 12.34 / 2.00 = 6.17
$result = IntPrecisionHelper::normDiv(1234, 200); // 617

// Addition
$result = IntPrecisionHelper::normAdd(1234, 567, 890); // 2691

// Subtraction
$result = IntPrecisionHelper::normSub(1234, 567); // 667

try {
    $result = IntPrecisionHelper::fromString("invalid");
} catch (InvalidArgumentException $e) {
    echo "Invalid input format: " . $e->getMessage();
}

try {
    $result = IntPrecisionHelper::fromString(""); // Empty string
} catch (InvalidArgumentException $e) {
    echo "Input cannot be empty";
}

// Universal normalize method with type validation
try {
    $result = IntPrecisionHelper::normalize([1, 2, 3]); // Array input
} catch (InvalidArgumentException $e) {
    echo "Invalid input type: " . $e->getMessage();
    // Output: "Input value must be a float, string, or int. Got array"
}

try {
    $result = IntPrecisionHelper::normalize("1.23e2"); // Scientific notation
} catch (InvalidArgumentException $e) {
    echo "Scientific notation not supported: " . $e->getMessage();
}

try {
    $result = IntPrecisionHelper::normDiv(1234, 0);
} catch (DivisionByZeroError $e) {
    echo "Cannot divide by zero";
}

try {
    $percentage = IntPrecisionHelper::calculatePercentage(50, 0);
} catch (DivisionByZeroError $e) {
    echo "Total cannot be zero for percentage calculation";
}

try {
    $result = IntPrecisionHelper::normMul(PHP_INT_MAX, PHP_INT_MAX);
} catch (OverflowException $e) {
    echo "Result would exceed maximum integer value";
}

// Standard precision (recommended for most use cases)
$result = IntPrecisionHelper::fromString("12.34");
$result = IntPrecisionHelper::normalize("12.34");

// Less precise but faster for very large numbers
$result = IntPrecisionHelper::fromString("12.34", true);
$result = IntPrecisionHelper::fromFloat(12.34, true);
$result = IntPrecisionHelper::normalize("12.34", true);
$result = IntPrecisionHelper::normalize(12.34, true);

class CustomPrecisionHelper extends IntPrecisionHelper
{
    protected const PRECISION_FACTOR = 1000; // 3 decimal places instead of 2
    protected const DECIMAL_PLACES = 3;
}

// Usage
$normalized = CustomPrecisionHelper::fromString("12.345"); // Returns: 12345
$display = CustomPrecisionHelper::toView(12345);           // Returns: "12.345"