HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpMinor

Library to convert any number to a metric expression

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
expressionnumberconvertanymetriclibrary

Problem

For a small personal project, I was looking to write a number as a human readable number. For example:

1023 -> 1K
12345678 -> 12M


Stack Exchange, like a lot of others websites, use the same trick. It is called metric prefixes.

Humanizer is a library to convert data in a human readable form. Since my feature match with their needs, I submitted a Pull Request (without answer for now). Meanwhile, I would love a feedback on my code.

What do you think of it? How can I improve it? This is the first time I have used XML doc. How is it?

```
using System;
using System.Collections.Generic;
using System.Linq;

namespace Humanizer
{
///
/// Contains extension methods for changing a number to Metric representation (ToMetric)
/// and from Metric representation back to the number (FromMetric)
///
public static class MetricNumeralExtensions
{
///
/// Symbols is a list of every symbols for the Metric system.
///
private static readonly char[][] Symbols =
{
new[] { 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' },
new[] { 'm', '\u03bc', 'n', 'p', 'f', 'a', 'z', 'y' }
};

///
/// Names link a Metric symbol (as key) to its name (as value).
///
///
/// We dont support :
/// {'h', "hecto"},
/// {'da', "deca" }, // !string
/// {'d', "deci" },
/// {'c', "centi"},
///
private static readonly Dictionary Names = new Dictionary()
{
{'Y', "yotta" }, {'Z', "zetta" }, {'E', "exa" }, {'P', "peta" }, {'T', "tera" }, {'G', "giga" }, {'M', "mega" }, {'k', "kilo" },
{'m', "milli" }, {'μ', "micro" }, {'n', "nano" }, {'p', "pico" }, {'f', "femto" }, {'a', "atto" }, {'z', "zepto" }, {'y', "yocto" }
};

///
/// Converts a Metric representation into a number.
///
///
/// We don't support input in the format {number}{name} nor {number} {name}.
/// We only provide a solution for {number}{symbol} and {number} {symbol}.
///
/// Metric representation to con

Solution

Since you specifically asked about doc comments, I love that you provided examples.

/// 
/// "1k".FromMetric() => 1000d
/// "123".FromMetric() => 123d
/// "100m".FromMetric() => 1E-1
/// 
/// A number after a conversion from a Metric representation.
public static double FromMetric(this string input)


But you missed something important. This won't render as code when you run it through Sandcastle or Doxygen. You have to wrap it in the `` element for it to render properly post processing.

This is the proper way to create example documentation.

/// 
/// ...maybe some plain text...
/// 
/// ...some example code here...
/// 
/// 


Just a general comment about the code itself; it's dense. Like, really dense. The readability would be greatly improved if you used more vertical white space.

Code Snippets

/// <example>
/// "1k".FromMetric() => 1000d
/// "123".FromMetric() => 123d
/// "100m".FromMetric() => 1E-1
/// </example>
/// <returns>A number after a conversion from a Metric representation.</returns>
public static double FromMetric(this string input)
/// <example>
/// ...maybe some plain text...
/// <code>
/// ...some example code here...
/// </ code>
/// </ example>

Context

StackExchange Code Review Q#105544, answer score: 3

Revisions (0)

No revisions yet.