-
Notifications
You must be signed in to change notification settings - Fork 323
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 Text.to_decimal #10874
Add Text.to_decimal #10874
Conversation
Convert this `Text` to a `Decimal`. | ||
|
||
Arguments: | ||
- scale: the optional Decimal scale to use. See `Decimal.set_scale` for more |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a way to link directly to other parts of our documentation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really at the moment.
We should use the Markdown syntax and could pick up in the website version and then think about adding to the doc panel as well.
Convert this `Text` to a `Decimal`. | ||
|
||
Arguments: | ||
- scale: the optional Decimal scale to use. See `Decimal.set_scale` for more |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really at the moment.
We should use the Markdown syntax and could pick up in the website version and then think about adding to the doc panel as well.
## ADVANCED | ||
GROUP Math | ||
ICON math | ||
Change the scale of a Decimal. | ||
|
||
A `Decimal` value is represented internally by a Java `BigInteger` "unscaled | ||
value" and a "scale value". The numerical value of the `Decimal` is | ||
`(unscaledValue * 10^(-scale))`. Scale values are maintained automatically by | ||
the constructors and numerical operations, but can also be set explicitly. | ||
|
||
Scale values can allow distinctions between values that would be identical as | ||
`Float`s. For example, the following values have different internal | ||
representations: | ||
|
||
a = Decimal.new "2.0" | ||
b = Decimal.new "2.00" | ||
a == b | ||
# => True | ||
|
||
These two values have different internal representations, but they are still | ||
considered the same value by `==`. | ||
|
||
! Error Conditions | ||
|
||
- If an explicit `scale` parameter is passed, and the scale is not | ||
large enough to represent the number exactly, an `Arithmetic_Error` | ||
is thrown. | ||
|
||
> Example | ||
Set the scale of a `Decimal` | ||
|
||
d = dec "23.456" set_scale 4 | ||
d.internal_representation | ||
# => [234560, 6, 4] | ||
|
||
> Example | ||
Get an error when using a scale that is too small. | ||
(A scale of 2 is not enough to represent three decimal places.) | ||
|
||
dec "23.456" set_scale 2 | ||
# => Arithmetic_Error | ||
set_scale : Integer -> Decimal | ||
set_scale self new_scale:Integer -> Decimal = | ||
handle_java_exception <| | ||
if self.scale == new_scale then self else | ||
Decimal.Value (self.big_decimal.setScale new_scale) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious: what is the use-case for making set_scale
public?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since Text.to_decimal
exposes the scale parameter, it makes sense to be consistent and expose this as well.
Pull Request Description
Add
Text.to_decimal
.Also makes renames
Decimal.with_scale
toset_scale
and makes it public.Checklist
Please ensure that the following checklist has been satisfied before submitting the PR:
Scala,
Java,
TypeScript,
and
Rust
style guides. In case you are using a language not listed above, follow the Rust style guide.