PetroSharp BigDecimal Unit Converter Tutorial, Features, and Code Examples
Precision is critical when building engineering, financial, or scientific applications. Standard floating-point types like float or double introduce rounding errors due to how computers handle binary math. The PetroSharp BigDecimal Unit Converter solves this problem by combining arbitrary-precision mathematics with a robust unit conversion engine.
This guide covers the core features of the library, walks you through a step-by-step tutorial, and provides practical code examples. Key Features
The PetroSharp BigDecimal Unit Converter is designed for industries where a single misplaced decimal point can cause catastrophic failures.
Arbitrary-Precision Math: Utilizes a custom BigDecimal implementation to eliminate the limitations and rounding errors of standard 64-bit float types.
Extensive Unit Registry: Supports diverse physical quantities including length, mass, pressure, temperature, flow rate, and viscosity.
Extensible Architecture: Allows developers to easily inject custom units or specific conversion factors without modifying the core library.
Thread-Safe Operations: Built from the ground up to support high-throughput, concurrent calculations in enterprise web services. Step-by-Step Tutorial
Getting started with the library requires minimal setup. Follow these steps to configure your project and run your first precise unit conversion. Step 1: Installation
Add the PetroSharp NuGet package to your project using the .NET CLI: dotnet add package PetroSharp.Mathematics.Units Use code with caution. Step 2: Initialize the Unit Converter
The library uses a registry pattern. You instantiate the specific converter class for the physical quantity you need to manipulate. Step 3: Define Values and Perform Conversion
Instantiate your source value using the BigDecimal type, specify the source unit, and call the conversion method targeting the destination unit. Code Examples
The following production-ready examples demonstrate how to handle common conversion scenarios with absolute precision. 1. Basic Pressure Conversion (Petroleum Engineering)
Petroleum engineering requires exact pressure calculations. Here is how to convert Pounds per Square Inch (psi) to Bars.
using PetroSharp.Mathematics; using PetroSharp.Units.Pressure; class Program { static void Main() { // Define a high-precision pressure value (1500.005 psi) BigDecimal psiValue = BigDecimal.Parse(“1500.005”); // Initialize the Pressure Converter var converter = new PressureConverter(); // Convert PSI to Bar BigDecimal barValue = converter.Convert(psiValue, PressureUnit.Psi, PressureUnit.Bar); Console.WriteLine(\("Exact Bar Value: {barValue}"); } } </code> Use code with caution. 2. Temperature Conversion with Offsets</p> <p>Temperature conversions involve both multiplication factors and scale offsets (like adding 273.15). The library maintains exactness across these boundaries.</p> <p><code>using PetroSharp.Mathematics; using PetroSharp.Units.Temperature; class Program { static void Main() { // 100.000000005 Degrees Celsius BigDecimal celsius = BigDecimal.Parse("100.000000005"); var converter = new TemperatureConverter(); // Convert Celsius to Kelvin BigDecimal kelvin = converter.Convert(celsius, TemperatureUnit.Celsius, TemperatureUnit.Kelvin); Console.WriteLine(\)“Exact Kelvin Value: {kelvin}”); } } Use code with caution. 3. Handling Mass Flow Rates in Loops
For high-performance calculations, such as processing sensor data streams, the converter remains fast and memory-efficient.
using PetroSharp.Mathematics; using PetroSharp.Units.MassFlow; class Program { static void Main() { var converter = new MassFlowConverter(); string[] rawSensorData = { “500.12345”, “500.12346”, “500.12347” }; foreach (var data in rawSensorData) { BigDecimal poundsPerHour = BigDecimal.Parse(data); BigDecimal kilogramsPerSecond = converter.Convert( poundsPerHour, MassFlowUnit.PoundsPerHour, MassFlowUnit.KilogramsPerSecond ); Console.WriteLine($“Converted: {kilogramsPerSecond} kg/s”); } } } Use code with caution. Best Practices
To get the most out of the library, keep these design patterns in mind:
Avoid Implicit Casts: Never initialize a BigDecimal from a double primitive variable (e.g., double x = 0.1; BigDecimal.From(x);). Doing so introduces the double’s precision loss into the library. Always initialize from a string or int.
Cache Converter Instances: Create a singleton or scoped instance of your converter classes in dependency injection containers rather than instantiating them on every function call.
Specify Rounding Modes: When displaying output to users, explicitly define your rounding preferences (such as RoundingMode.HalfUp) at the final presentation layer to keep the underlying core data pure. To help me tailor the next steps, tell me:
What programming language or .NET version are you targeting?
Leave a Reply