{"id":21926574,"url":"https://github.com/astzweig/swift-digits","last_synced_at":"2025-08-18T11:05:23.006Z","repository":{"id":192152896,"uuid":"631107353","full_name":"astzweig/swift-digits","owner":"astzweig","description":"🦁 A Swift library for working with integer digits.","archived":false,"fork":false,"pushed_at":"2023-04-22T23:45:09.000Z","size":17,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-01T05:03:20.990Z","etag":null,"topics":["calculations","fundamental","library","numbers","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"eupl-1.2","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/astzweig.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}},"created_at":"2023-04-22T00:40:35.000Z","updated_at":"2024-07-12T03:05:58.000Z","dependencies_parsed_at":"2023-09-03T00:54:31.476Z","dependency_job_id":null,"html_url":"https://github.com/astzweig/swift-digits","commit_stats":null,"previous_names":["astzweig/swift-digits"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/astzweig/swift-digits","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astzweig%2Fswift-digits","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astzweig%2Fswift-digits/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astzweig%2Fswift-digits/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astzweig%2Fswift-digits/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/astzweig","download_url":"https://codeload.github.com/astzweig/swift-digits/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astzweig%2Fswift-digits/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270982194,"owners_count":24679447,"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","status":"online","status_checked_at":"2025-08-18T02:00:08.743Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["calculations","fundamental","library","numbers","swift"],"created_at":"2024-11-28T22:08:56.216Z","updated_at":"2025-08-18T11:05:22.973Z","avatar_url":"https://github.com/astzweig.png","language":"Swift","readme":"# Swift Digits\n\nSwift Digits extends Swift's builtin integer types with useful methods that work on individual digits.\n\n## Usage\nDigits are always relativ to a [base] of a [positional system]. All methods therefor\ntake a `usingBase` parameter, which defaults to the base 10 i.e. the decimal system.\n\n[base]: https://en.wikipedia.org/wiki/Radix\n[positional system]: https://en.wikipedia.org/wiki/Positional_notation#Mathematics\n\n\u003cdetails\u003e\n\u003csummary\u003eTable of contents\u003c/summary\u003e\n    \n- [Digits amount](#digits-amount)\n- [Digits sum](#digits-sum)\n- [Digits reversion](#digits-reversion)\n- [Digits replacement](#digits-replacement)\n- [Digits iterator](#digits-iterator)\n- [Digits array](#digits-array)\n- [Highest positional factor](#highest-positional-factor)\n- [Digits iterator](#digits-iterator)\n\n\u003c/details\u003e\n\n### Digits amount\nCounts the digits of the number.\n\n```swift\nlet _ = 2014.countDigits() // returns 4\nlet _ = 2014.countDigits(usingBase: 16) // returns 3, as 2014 = 0x7de\n```\n\n### Digits sum\nCalculats the sum of the digits.\n\n```swift\nlet _ = 2014.sumDigits() // returns 7, as 7 = 2 + 0 + 1 + 4\nlet _ = 0x7de.sumDigits(usingBase: 16) // returns 34 or 0x22, as 0x22 = 0x7 + 0xd + 0xe\n```\n\n### Digits reversion\nReverses the digit order.\n\n```swift\nlet _ = 2014.revertDigits() // returns 4102\nlet _ = 0x7de.revertDigits(usingBase: 16) // returns 3799 or 0xed7\n```\n\n### Digits inversion\nInvert each digit by substracting it from the highest possible digit, i.e.\n9 in the decimal system. So 0 becomes 9 = 9 - 0, 1 becomes 8 = 9 - 1, etc..\n\n```swift\nlet _ = 2014.invertDigits() // returns 7985\nlet _ = 0x7de.invertDigits(usingBase: 16) // returns 2081 or 0x821\n```\n\n### Digits replacement\nReplaces all occurences of a digit with another digit.\n```swift\nvar counter = 1010\ncounter.replaceDigit(0, with: 1)\n// counter == 1111\n\nvar hexCounter = 0xf0\nhexCounter.replaceDigit(0xf, with: 0xc, usingBase: 16)\n// counter == 0xc0 or 192\n```\n\nAlternatively there is a non mutating method:\n\n```swift\nvar counter = 1010.replacingDigit(0, with: 1)\n// counter == 1111\n```\n\n### Digits iterator\nReturns an iterator that goes over digits.\n\n```swift\nfor digit in 2014.digits() {\n    print(digit)\n}\n// prints\n// 2\n// 0\n// 1\n// 4\n\nfor digit in 0x7de.digits(usingBase: 16) {\n    print(digit)\n}\n// prints\n// 7\n// 13 or 0xd\n// 14 or 0xe\n```\n\n### Digits array\nReturns an array containing the digits of the number with the most significant\ndigit at the beginning.\n\n```swift\nlet _ = 2014.asDigits() // returns [2, 0, 1, 4]\nlet _ = 0xfde.asDigits(usingBase: 16) // returns [15 or 0xf, 13 or 0xd, 14 or 0xe]\n```\n\n### Highest positional factor\nReturns the highest positional factor included in the number, i.e. the factor of\nthe most significant digit.\n\n```swift\nlet _ = 12.highestPositionalFactor() // returns 10, as 12 = 1 * 10 + 2 * 1\nlet _ = 933.highestPositionalFactor()) // returns 100, as 933 = 9 * 100 + 3 * 10 + 3 * 1\n\nlet _ = 0xc.highestPositionalFactor(usingBase: 16)) // returns 1, as 0xc = 12 * 1\nlet _ = 0x5d.highestPositionalFactor(usingBase: 16)) // returns 16, as 0x5d = 5 * 16 + 13 * 1\n```\n\n## Adding `Digits` as a Dependency\n\nTo use the `Digits` library in a SwiftPM project, \nadd it to the dependencies for your package and your command-line executable target:\n\n```swift\nlet package = Package(\n    // name, platforms, products, etc.\n    dependencies: [\n        // other dependencies\n        .package(url: \"https://github.com/astzweig/swift-digits\", from: \"1.0.0\"),\n    ],\n    targets: [\n        .executableTarget(name: \"\u003ccommand-line-tool\u003e\", dependencies: [\n            // other dependencies\n            .product(name: \"Digits\", package: \"swift-digits\"),\n        ]),\n        // other targets\n    ]\n)\n```\n\n### Supported Versions\n\nThe most recent versions of swift-digits supports Swift 5.5 and newer. The minimum Swift version supported by swift-digits releases are detailed below:\n\nswift-digits          | Minimum Swift Version\n----------------------|----------------------\n`0.0.1 ...`           | 5.5\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastzweig%2Fswift-digits","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fastzweig%2Fswift-digits","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastzweig%2Fswift-digits/lists"}