{"id":21277673,"url":"https://github.com/atmoos/quantities","last_synced_at":"2025-07-11T08:32:01.975Z","repository":{"id":142748773,"uuid":"570904601","full_name":"atmoos/Quantities","owner":"atmoos","description":"Library that enables typesafe representation of quantities.","archived":false,"fork":false,"pushed_at":"2024-05-20T08:29:39.000Z","size":1557,"stargazers_count":3,"open_issues_count":19,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-15T20:41:02.806Z","etag":null,"topics":["conversion","csharp","csharp-library","measure","measurements","measures","quantities","quantity","units","units-converter","units-measures-converter","units-of-measure","units-of-measurement","unitsofmeasurement"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/atmoos.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-26T14:08:31.000Z","updated_at":"2024-05-19T10:44:05.000Z","dependencies_parsed_at":"2023-09-30T03:31:07.394Z","dependency_job_id":"37ddf212-2e70-436b-8c1f-bcc37e8c94a6","html_url":"https://github.com/atmoos/Quantities","commit_stats":{"total_commits":356,"total_committers":2,"mean_commits":178.0,"dds":"0.011235955056179803","last_synced_commit":"68ef9c62df841d3257aeb15106fdb434c6fafecf"},"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atmoos%2FQuantities","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atmoos%2FQuantities/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atmoos%2FQuantities/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atmoos%2FQuantities/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/atmoos","download_url":"https://codeload.github.com/atmoos/Quantities/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225708290,"owners_count":17511635,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conversion","csharp","csharp-library","measure","measurements","measures","quantities","quantity","units","units-converter","units-measures-converter","units-of-measure","units-of-measurement","unitsofmeasurement"],"created_at":"2024-11-21T10:06:48.031Z","updated_at":"2024-11-21T10:06:49.416Z","avatar_url":"https://github.com/atmoos.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003c!-- markdownlint-disable MD033 MD041 --\u003e\n\u003cdiv align=\"center\"\u003e\n \u003cimg src=\"./assets/images/atmoos.quantities.svg\" height=\"160\" alt=\"Logo\"\u003e\n\u003c/div\u003e\n\u003c!-- markdownlint-enable MD033 MD041 --\u003e\n\n# Atmoos Quantities\n\nA library to safely handle various types of quantities, typically physical quantities.\n\n[![master status](https://github.com/atmoos/Quantities/actions/workflows/dotnet.yml/badge.svg)](https://github.com/atmoos/Quantities/actions/workflows/dotnet.yml)\n[![nuget package](https://img.shields.io/nuget/v/Atmoos.Quantities.svg?logo=nuget)](https://www.nuget.org/packages/Atmoos.Quantities)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/atmoos/Quantities/blob/master/LICENSE)\n\n## Examples\n\nUsage is designed to be intuitive:\n\n- Instantiation *of* quantities with static factory methods\n  - `Quantity.Of(42, Metric\u003cUnit\u003e()): Quantity`\n- Conversion *to* other units with instance conversion methods\n  - `quantity.To(Imperial\u003cUnit\u003e())`\n\n### Instantiation\n\nSomewhere in your project, define this global static using directive:\n\n```csharp\nglobal using static Atmoos.Quantities.Systems;\n```\n\nThen, use quantities intuitively:\n\n```csharp\nLength metres = Length.Of(4, Si\u003cMetre\u003e());\nLength miles = Length.Of(12, Imperial\u003cMile\u003e());\nLength kilometres = Length.Of(18, Si\u003cKilo, Metre\u003e());\nVelocity kilometresPerHour = Velocity.Of(4, Si\u003cKilo, Metre\u003e().Per(Metric\u003cHour\u003e()));\n```\n\n### Conversion\n\n```csharp\nLength miles = metres.To(Imperial\u003cMile\u003e());\nLength kilometres = metres.To(Si\u003cKilo, Metre\u003e());\nVelocity metresPerSecond = kilometresPerHour.To(Si\u003cMetre\u003e().Per(Si\u003cSecond\u003e()));\nVelocity milesPerHour = kilometresPerHour.To(Imperial\u003cMile\u003e().Per(Metric\u003cHour\u003e()));\n```\n\n### Operator Overloads\n\nQuantities support common operations such as addition, subtraction, multiplication and division. The operations are \"left associative\", meaning the units of the left operand are \"carried over\" to the result when possible.\n\n```csharp\nTime time = Time.Of(3, Metric\u003cHour\u003e());\n\nVelocity metricVelocity = kilometres / time; // 6 km/h\nVelocity imperialVelocity = miles / time; // 4 mi/h\n\nArea metricArea = kilometres * miles; // 347.62 km²\nArea imperialArea = miles * kilometres; // 134.22 mi²\nConsole.WriteLine($\"Equal area: {metricArea.Equals(imperialArea)}\"); // Equal area: True\n\nLength metricSum = kilometres + miles - metres; // 37.308 km\nLength imperialSum = miles + kilometres - metres; // 23.182 mi\nConsole.WriteLine($\"Equal length: {imperialSum.Equals(metricSum)}\"); // Equal length: True\n```\n\n### Type Safety\n\nAs one of the primary goals it to ensure safety when using quantities, type safety is essential.\n\nAdditive operations only work on instances of the same type\n\n```csharp\nPower power = Power.Of(36, Si\u003cWatt\u003e());\nMass mass = Mass.Of(0.2, Metric\u003cTonne\u003e());\n\n// Doesn't compile:\n// Cannot implicitly convert type 'double' to 'Power'\nMass foo = mass + power;\nPower bar = power + mass;\n```\n\nMultiplication of different quantities is very common, hence compile errors are less frequent. Type safety is always ensured, though.\n\n```csharp\n// Common operation: Ohm's Law\nElectricCurrent ampere = ElectricCurrent.Of(3, Si\u003cAmpere\u003e());\nElectricalResistance ohm = ElectricalResistance.Of(7, Si\u003cOhm\u003e());\n\n// U = R * I\n// The multiplicative result is a different type: ElectricPotential\nElectricPotential potential = ohm * ampere; // 21 V\n\n// Eccentric operation\nTime time = Time.Of(5, Metric\u003cHour\u003e());\nMass mass = Mass.Of(0.2, Metric\u003cTonne\u003e());\n\n// Doesn't compile\n// Operator '*' is ambiguous on operands of type 'Mass' and 'Time'\nMass foo = mass * time;\nTime bar = time * mass;\nvar fooBar = mass * time;\n```\n\n### Binary Prefixes\n\nDifferent types of prefixes are also supported. This is useful for [IEC binary prefixes](https://en.wikipedia.org/wiki/Binary_prefix).\n\n```csharp\nData kibiByte = Data.Of(1, Binary\u003cKibi, Byte\u003e()); // 1 KiB, binary prefix\nData kiloByte = Data.Of(1.024, Metric\u003cKilo, Byte\u003e()); // 1 kB, metric prefix\nConsole.WriteLine($\"Equal amount of data: {kiloByte.Equals(kibiByte)}\"); // Equal amount of data: True\n```\n\n## Should I use this Library?\n\nYes. The API has stabilised. Furthermore, the library outperforms naive implementations both in terms of performance and accuracy. Additionally, any combination of prefix and unit is supported out of the box for si and metric quantities.\n\nAlso, this library supports serialization for json with both `System.Text.Json` and `Newtonsoft.Json`. If a specific form of serialization is required, the library provides api's to extend it with custom serialization. (See [write](./source/Quantities/Core/Serialization/IWriter.cs) and [read](./source/Quantities/Serialization/QuantityFactory.cs) support.)\n\n## Thanks\n\n- to contributors for providing feedback and ideas.\n- to my sister [Lucy Kägi](https://www.lucykaegi.ch/) for creating the [logo](assets/images/atmoos.quantities.svg).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatmoos%2Fquantities","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fatmoos%2Fquantities","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatmoos%2Fquantities/lists"}