Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Globalization;
- using System.Text;
- #pragma warning disable CA1822
- namespace TransformToWordsTask
- {
- /// <summary>
- /// Provides transformer methods.
- /// </summary>
- public sealed class Transformer
- {
- /// <summary>
- /// Converts number's digital representation into words.
- /// </summary>
- /// <param name="number">Number to convert.</param>
- /// <returns>Words representation.</returns>
- public string TransformToWords(double number)
- {
- switch (number)
- {
- case double.NegativeInfinity: return "Negative Infinity";
- case double.PositiveInfinity: return "Positive Infinity";
- case double.Epsilon: return "Double Epsilon";
- case double.NaN: return "NaN";
- }
- string stringRepresentation = number.ToString(CultureInfo.InvariantCulture);
- string result = String.Empty;
- for (int i = 0; i < stringRepresentation.Length; i++)
- {
- result += GetWordRepresentation(stringRepresentation[i])) + ' ';
- }
- return char.ToUpper(result[0], CultureInfo.InvariantCulture) + result[1..^1];
- }
- private static string GetWordRepresentation(char character)
- {
- return character switch
- {
- '-' => "minus",
- '1' => "one",
- '2' => "two",
- '3' => "three",
- '4' => "four",
- '5' => "five",
- '6' => "six",
- '7' => "seven",
- '8' => "eight",
- '9' => "nine",
- '0' => "zero",
- '.' => "point",
- 'E' => "E",
- '+' => "plus",
- _ => null
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement