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

Decimal with X number of digits after decimal #332

Open
xadvfh opened this issue Oct 14, 2020 · 2 comments
Open

Decimal with X number of digits after decimal #332

xadvfh opened this issue Oct 14, 2020 · 2 comments

Comments

@xadvfh
Copy link

xadvfh commented Oct 14, 2020

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

  • Is the feature something that currently cannot be done?
    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.

  • What alternatives have you considered?

Finance.Amount

@bchavez
Copy link
Owner

bchavez commented Oct 16, 2020

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;
   }
}

image


As far as getting a code change in Bogus; I could see int? decimals = null as an optional parameter for these precision types here:

/// <summary>
/// Get a random double, between 0.0 and 1.0.
/// </summary>
/// <param name="min">Minimum, default 0.0</param>
/// <param name="max">Maximum, default 1.0</param>
public double Double(double min = 0.0d, double max = 1.0d)
{
//lock any seed access, for thread safety.
lock( Locker.Value )
{
if( min == 0.0d && max == 1.0d )
{
//use default implementation
return localSeed.NextDouble();
}
return localSeed.NextDouble() * (max - min) + min;
}
}
/// <summary>
/// Get a random decimal, between 0.0 and 1.0.
/// </summary>
/// <param name="min">Minimum, default 0.0</param>
/// <param name="max">Maximum, default 1.0</param>
public decimal Decimal(decimal min = 0.0m, decimal max = 1.0m)
{
return Convert.ToDecimal(Double()) * (max - min) + min;
}
/// <summary>
/// Get a random float, between 0.0 and 1.0.
/// </summary>
/// <param name="min">Minimum, default 0.0</param>
/// <param name="max">Maximum, default 1.0</param>
public float Float(float min = 0.0f, float max = 1.0f)
{
return Convert.ToSingle(Double() * (max - min) + min);
}

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,
Brian

RE: #319, #320

@logiclrd
Copy link
Contributor

logiclrd commented Dec 8, 2020

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants