{"id":36604304,"url":"https://github.com/labai/deci","last_synced_at":"2026-01-12T08:42:11.167Z","repository":{"id":63855280,"uuid":"432527602","full_name":"labai/deci","owner":"labai","description":"deci - decimals w/o tricks","archived":false,"fork":false,"pushed_at":"2025-12-27T10:27:06.000Z","size":143,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-28T01:33:45.398Z","etag":null,"topics":["datatype","kotlin","kotlin-library"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","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/labai.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-11-27T17:43:08.000Z","updated_at":"2025-12-27T10:27:09.000Z","dependencies_parsed_at":"2023-11-15T20:42:49.320Z","dependency_job_id":null,"html_url":"https://github.com/labai/deci","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/labai/deci","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/labai%2Fdeci","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/labai%2Fdeci/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/labai%2Fdeci/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/labai%2Fdeci/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/labai","download_url":"https://codeload.github.com/labai/deci/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/labai%2Fdeci/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28337599,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T06:09:07.588Z","status":"ssl_error","status_checked_at":"2026-01-12T06:05:18.301Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["datatype","kotlin","kotlin-library"],"created_at":"2026-01-12T08:42:11.106Z","updated_at":"2026-01-12T08:42:11.154Z","avatar_url":"https://github.com/labai.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Deci – a decimal class for Kotlin without tricks\n\nWorking with decimals in Java is unpleasant.  \nThe `float` and `double` classes can be used, but they rely on floating-point arithmetic and are not recommended when fixed-point precision is required (e.g., in financial calculations).  \nFor such cases, the `BigDecimal` class is typically used.\n\nUnfortunately, code written with `BigDecimal` is often verbose and difficult to read. In addition, `BigDecimal` has several pitfalls (e.g., `equals` behavior, loss of scale during division).\n\n*Kotlin* provides operators (`-`, `+`, `*`, `/`), which should improve readability. However, some problems inherited from `BigDecimal` still remain. For example:\n- Division (`div`) uses the original scale and applies `HALF_EVEN` rounding, which is not suitable in most situations. As a result, the division operator (`/`) often cannot be used directly in formulas.\n- Equality checks take scale into account, so `2.0 != 2.00`. Therefore, `compareTo` must be used instead of `==`.\n\nTo address these issues, the *Deci* class can be used.  \nThe idea is to create a simple `BigDecimal` wrapper that behaves slightly differently:\n- Uses `HALF_UP` rounding\n- Produces division results with a high scale\n- Provides additional math operators with `BigDecimal`, `Int`, and `Long`\n- Equality (`==`) ignores scale\n\nAdditional functions:\n- `round` – rounds a number to the specified number of decimal places and returns a `Deci`\n- `eq` – compares numbers of various types (including `null`)\n- `BigDecimal`, `Int`, and `Long` have `.deci` extension functions to convert values to *Deci*\n\n## What it isn't\nDeci does not reinvent math algorithms - at its core, it reuses Java’s `BigDecimal`.\n\n## Math in code\n\nWith _Deci_, you can use operators, making formulas easier to read compared to method calls with _BigDecimal_.\n\n```kotlin\nval result = (price * quantity - fee) * 100 / (price * quantity) round 2\n```\n\n## BigDecimal vs Deci examples\n\n### 1. Equals \n\nYou would expect numbers to be equal regardless of trailing decimal zeros.\nThis is not true for _BigDecimal_:\n\n```kotlin\nprintln(BigDecimal(\"1.0\") == BigDecimal(\"1\"))\n```\n\u003e false\n\nWith _BigDecimal_, you must use `compareTo` instead of `equals`.\nWith _Deci_, the behavior is as expected:\n\n```kotlin\nprintln(Deci(\"1.0\") == Deci(\"1\"))\n```\n\u003e true\n\n### 2. Dividing \n\n_BigDecimal_ keeps the scale of the first operand when dividing:\n\n```kotlin\n   println(BigDecimal(\"5\") / BigDecimal(\"2\"))\n```\n\u003e 2\n\n_Deci_ uses a high scale (up to 20 decimal places), which is sufficient for most real-world cases.\n\n```kotlin\n   println(5.deci / 2.deci)\n```\n\u003e 2.5\n\n```kotlin\n   println(100000.deci / 3.deci)\n```\n\u003e 33333.33333333333333333333\n\n```kotlin\n   println(Deci(\"0.00001\") / 3.deci)\n```\n\u003e 0.0000033333333333333333333\n\n### 3. Rounding\n\n_BigDecimal_ uses half-even rounding by default, while _Deci_ uses half-up rounding, which is more common.\n\n```kotlin\nprintln(BigDecimal(\"2.5\") / BigDecimal(\"2\"))\n```\n\u003e 1.2\n\n```kotlin\nprintln(Deci(\"2.5\") / Deci(\"2\") round 1)\n```\n\u003e 1.3\n\n\n### Usage\n\nAdd the Maven dependency:\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eMaven (jvm)\u003c/strong\u003e\u003c/summary\u003e\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.github.labai\u003c/groupId\u003e\n  \u003cartifactId\u003edeci-jvm\u003c/artifactId\u003e\n  \u003cversion\u003e0.0.2\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eGradle for kotlin multiplatform\u003c/strong\u003e\u003c/summary\u003e\n\n```kotlin\nsourceSets {\n    commonMain.dependencies {\n        implementation(\"com.github.labai:deci:0.0.2\")\n    }\n}\n```\n\u003c/details\u003e\n\n## deci.kt\n[more info](../../tree/main/deci) \n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flabai%2Fdeci","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flabai%2Fdeci","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flabai%2Fdeci/lists"}