-
-
Notifications
You must be signed in to change notification settings - Fork 512
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
Decimal with X number of digits after decimal #332
Comments
Hi @xadvfh, I think, for now, you'd want to use a C# extension method workaround for readability as demonstrated below: void Main()
{
var account = new Faker<Account>()
.RuleFor(a => a.InterestRate, f => f.Random.Decimal(1, 2, 4))
.Generate();;
account.Dump();
}
public static class ExtensionsForRandomizer
{
public static decimal Decimal(this Randomizer r, decimal min = 0.0m, decimal max = 1.0m, int? decimals = null)
{
var value = r.Decimal(min, max);
if( decimals.HasValue ){
return Math.Round(value, decimals.Value);
}
return value;
}
} As far as getting a code change in Bogus; I could see Bogus/Source/Bogus/Randomizer.cs Lines 170 to 208 in 7caf7e0
Your request kind of makes sense, but I'd have to think about it a little more and let it bake. I'm super careful when making changes to public APIs just because of the sheer amount of downloads involved. Once a change is deployed, it's hard to walk it back! But I'm leaning toward making the change. Thank you for taking the time to make your issue known! Thanks, |
I mean, it could be added as a separate overload so that existing calls keep going to the same method they've been calling all along, right? |
Please describe why you are requesting a feature
I'm not sure if this is already provided but I couldn't find it. Is there a way to get a random decimal and specify the number of digits after the decimal? I can use the
Finance.Amount
option but amount seems weird to me.Please answer any or all of the questions below
It can but through a method name that makes the code read awkwardly. For example:
RuleFor(account=> account.InterestRate, f => f.Finance.Amount(min: 1, max: 5, decimals: 3));
I can wrap the method myself but was wondering if it makes sense to do that within Bogus.Finance.Amount
The text was updated successfully, but these errors were encountered: