Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add number round #3533

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/quick-roses-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"effect": minor
"@effect/cli": patch
---

Add Number.round
8 changes: 2 additions & 6 deletions packages/cli/src/internal/prompt/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as Optimize from "@effect/printer/Optimize"
import * as Schema from "@effect/schema/Schema"
import * as Arr from "effect/Array"
import * as Effect from "effect/Effect"
import * as EffectNumber from "effect/Number"
import * as Option from "effect/Option"
import type * as Prompt from "../../Prompt.js"
import * as InternalPrompt from "../prompt.js"
Expand All @@ -20,11 +21,6 @@ interface State {
readonly error: Option.Option<string>
}

const round = (number: number, precision: number) => {
const factor = Math.pow(10, precision)
return Math.round(number * factor) / factor
}

const parseInt = Schema.NumberFromString.pipe(
Schema.int(),
Schema.decodeUnknown
Expand Down Expand Up @@ -352,7 +348,7 @@ function handleProcessFloat(options: FloatOptions) {
})),
onSuccess: (n) =>
Effect.flatMap(
Effect.sync(() => round(n, options.precision)),
Effect.sync(() => EffectNumber.round(n, options.precision)),
(rounded) =>
Effect.match(options.validate(rounded), {
onFailure: (error) =>
Expand Down
23 changes: 23 additions & 0 deletions packages/effect/src/Number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,3 +492,26 @@ export const parse = (s: string): Option<number> => {
? option.none
: option.some(n)
}

/**
* Returns the number rounded with the given precision.
*
* @param self - The number to round
* @param precision - The precision
*
* @example
* import { round } from "effect/Number"
*
* assert.deepStrictEqual(round(5,1234, 2), 5.12)
* assert.deepStrictEqual(nextPow2(17), 32)
Copy link
Member

@tim-smart tim-smart Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed (nextPow2).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for that. I started to write the doc and stopped to write the tests first and forgot to come back to it.
There was also a mistake on the first line, by the way

*
* @category math
* @since 3.8.0
*/
export const round: {
(precision: number): (self: number) => number
(self: number, precision: number): number
} = dual(2, (self: number, precision: number): number => {
const factor = Math.pow(10, precision)
return Math.round(self * factor) / factor
})
9 changes: 9 additions & 0 deletions packages/effect/test/Number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,13 @@ describe("Number", () => {
assert.deepStrictEqual(Number.parse("42"), Option.some(42))
assert.deepStrictEqual(Number.parse("a"), Option.none())
})

it("round", () => {
assert.deepStrictEqual(Number.round(1.1234, 2), 1.12)
assert.deepStrictEqual(Number.round(2)(1.1234), 1.12)
assert.deepStrictEqual(Number.round(0)(1.1234), 1)
assert.deepStrictEqual(Number.round(0)(1.1234), 1)
assert.deepStrictEqual(Number.round(1.567, 2), 1.57)
assert.deepStrictEqual(Number.round(2)(1.567), 1.57)
})
})
Loading