-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
[API Proposal]: Scale property for the decimal datatype #65074
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsBackground and motivationTo quickly and easily get the different parts (the whole number and the fractional part) of a decimal. Also allows to determine the length of the different parts. API Proposalnamespace System
{
public struct Decimal // : ...
{
+ public int WholeNumber => // The whole number part (before the decimal separator)
+ public int FractionalPart => // The decimal part (after the decimal separator)
}
} API Usagedecimal foo = 12.34m;
Console.WriteLine(foo.WholeNumber); // 12
Console.WriteLine(foo.FractionalPart); // 34 decimal foo = 1.2345m;
Console.WriteLine(foo.WholeNumber.ToString().Length); // 1
Console.WriteLine(foo.FractionalPart.ToString().Length); // 4 Alternative DesignsNo response RisksNo response
|
|
The signature as given wouldn't work/be useful, IMO. There are cases where breaking it into However, there are also limitations with how |
You're right, it wouldn't be useful as an integer as it could not differentiate between Yes, I can get the whole part easily by casting, but the decimal part is trickier. So I thought some properties could come in handy and be easier to use. We have a web application where the users enter quantity (of invoice articles) and wanted to count the amount of decimals for data validation because a third-party API has a limit of three decimals.
I agree. It would have to be some other data type than |
I think an an efficient and still fairly clear way to do that is |
I'd agree with svick here. If all you're wanting to do is validation or ensuring user inputs meet the requirements, then there are much better ways to do that then trying to get the integer and fractional parts. If you want to get something that's rounded to 3 digits, you have Given that this is If you do want to split into just integer/fractional parts; you can simply: var integerPart = decimal.Truncate(x);
var fractionalPart = x - integerPart; |
I've rewritten the API proposal to instead provide a |
This seems like a reasonable thing to expose, uses the existing terminology from the existing constructor, and is a simple extract of the underlying data. |
Related: Although this issue was closed, these proposed features still seem like a good idea to me. |
We had explicitly determined
If we want to have concepts like these, then we are likely better off writing up a proposal and implementing the IEEE 754 |
namespace System
{
public partial struct Decimal
{
public byte Scale { get; }
}
} |
@vanillajonathan are you interested in offering a PR or should we mark up for grabs? |
@danmoseley Mark it as up for grabs. |
Could I be assigned to this? I'll do it now. |
Assigned out, thanks @MichalPetryka! -- Noting we already discussed the needs/implementation here (new API, doc comment, and basic tests) in the C# Community Discord. |
Adds System.Decimal.Scale, a property that returns the scaling factor of the decimal. Closes dotnet#65074.
Adds System.Decimal.Scale, a property that returns the scaling factor of the decimal. Closes #65074.
Background and motivation
To quickly and easily get the number of decimal places of a decimal. Like
SqlDecimal
.API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: