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

Number to number extensions #518

Closed
i3arnon opened this issue Jan 30, 2016 · 5 comments
Closed

Number to number extensions #518

i3arnon opened this issue Jan 30, 2016 · 5 comments
Labels

Comments

@i3arnon
Copy link
Contributor

i3arnon commented Jan 30, 2016

I think it would be nice to add to the convenient extensions for TimeSpan, ByteSize extensions that simplify writing big numbers, for example:

int size = 20.Millions();

Instead of:

int size = 20000000;

It's clearer to read, simpler to write and reduces the chance for errors.

I plan on using this extensively in my configuration defaults (e.g. BounedCapacity = 10.Millions(), MaxDegreeOfParallelism = 1.Thousands())

i3arnon added a commit to i3arnon/Humanizer that referenced this issue Jan 30, 2016
@aloisdg
Copy link
Contributor

aloisdg commented Jan 31, 2016

In another hand, you could use

var size = "20M".FromMetric(); // 20 millions

or

var size = "20k".FromMetric(); // 20 thousands

It will works as expected.

@mexx mexx mentioned this issue Jan 31, 2016
@aloisdg
Copy link
Contributor

aloisdg commented Jan 31, 2016

And if you really want to use words:

private static T UseMetric<T>(this T input, string unit)
{
    return (input + unit).FromMetric();
}

public static long Millions(this long input)
{
    return input.UseMetric<long>("M");
}

If you dont want to use "Metric":

private static T MultiplyByTen<T>(this T input, string unit)
{
    return input * Math.Pow(10, unit);
}

public static long Millions(this long input)
{
    return input.MultiplyByTen(6);
}

My only concern for both is performance.

@mexx
Copy link
Collaborator

mexx commented Jan 31, 2016

From the performance point of view the proposed implementation in #519 is pretty good, as it only uses simple multiplication with a constant. Further the proposal provides implementations for the common numeric types, where FromMetric only returns double.

@aloisdg
Copy link
Contributor

aloisdg commented Jan 31, 2016

From the performance point of view the proposed implementation in #519 is pretty good

I know. This is why I added "My only concern for both is performance." at the end of my post. "Both" are UseMetric and MultiplyByTen.

FromMetric only returns double

I can change that if you think it is worth it.

Sorry If my comments were mistaken. I just share some alternatives. I think #519 is a good idea. The implementation is simple, stupid and works very well. 👍

@mexx
Copy link
Collaborator

mexx commented Jan 31, 2016

I can change that if you think it is worth it.

I wonder how you would achieve this, as the method can't be overloaded only by changing the result type.

You don't have to be sorry, to be able to discuss on alternatives is always good 👍. It drives learning other approaches to the problem and an alternative can be a good fit in some other scenario for anyone reading this discussion someday.

@hazzik hazzik added the jump in label Feb 9, 2016
i3arnon added a commit to i3arnon/Humanizer that referenced this issue Feb 13, 2016
@aloisdg aloisdg mentioned this issue Jun 27, 2016
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants