mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Correct formatting of numbers in output
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using Games.Common.Numbers;
|
||||||
using System.IO;
|
|
||||||
|
|
||||||
namespace Games.Common.IO;
|
namespace Games.Common.IO;
|
||||||
|
|
||||||
@@ -76,17 +75,16 @@ public interface IReadWrite
|
|||||||
void WriteLine(string message = "");
|
void WriteLine(string message = "");
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes a <see cref="float" /> to output, formatted per the BASIC interpreter, with leading and trailing spaces.
|
/// Writes a <see cref="Number" /> to output.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">The <see cref="float" /> to be written.</param>
|
/// <param name="value">The <see cref="Number" /> to be written.</param>
|
||||||
void Write(float value);
|
void Write(Number value);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes a <see cref="float" /> to output, formatted per the BASIC interpreter, with leading and trailing spaces,
|
/// Writes a <see cref="Number" /> to output.
|
||||||
/// followed by a new-line.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">The <see cref="float" /> to be written.</param>
|
/// <param name="value">The <see cref="Number" /> to be written.</param>
|
||||||
void WriteLine(float value);
|
void WriteLine(Number value);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Writes an <see cref="object" /> to output.
|
/// Writes an <see cref="object" /> to output.
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
using System;
|
using Games.Common.Numbers;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Games.Common.IO;
|
namespace Games.Common.IO;
|
||||||
|
|
||||||
@@ -100,9 +97,9 @@ public class TextIO : IReadWrite
|
|||||||
|
|
||||||
public void WriteLine(string value = "") => _output.WriteLine(value);
|
public void WriteLine(string value = "") => _output.WriteLine(value);
|
||||||
|
|
||||||
public void Write(float value) => _output.Write(GetString(value));
|
public void Write(Number value) => _output.Write(value.ToString());
|
||||||
|
|
||||||
public void WriteLine(float value) => _output.WriteLine(GetString(value));
|
public void WriteLine(Number value) => _output.WriteLine(value.ToString());
|
||||||
|
|
||||||
public void Write(object value) => _output.Write(value.ToString());
|
public void Write(object value) => _output.Write(value.ToString());
|
||||||
|
|
||||||
|
|||||||
20
00_Common/dotnet/Games.Common/Numbers/Number.cs
Normal file
20
00_Common/dotnet/Games.Common/Numbers/Number.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
namespace Games.Common.Numbers;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A single-precision floating-point number with string formatting equivalent to the BASIC interpreter.
|
||||||
|
/// </summary>
|
||||||
|
public struct Number
|
||||||
|
{
|
||||||
|
private readonly float _value;
|
||||||
|
|
||||||
|
public Number (float value)
|
||||||
|
{
|
||||||
|
_value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator float(Number value) => value._value;
|
||||||
|
|
||||||
|
public static implicit operator Number(float value) => new Number(value);
|
||||||
|
|
||||||
|
public override string ToString() => _value < 0 ? $"{_value} " : $" {_value} ";
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
using Chief.Resources;
|
|
||||||
using static Chief.Resources.Resource;
|
using static Chief.Resources.Resource;
|
||||||
|
|
||||||
namespace Chief;
|
namespace Chief;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ namespace Chief;
|
|||||||
|
|
||||||
internal static class IReadWriteExtensions
|
internal static class IReadWriteExtensions
|
||||||
{
|
{
|
||||||
internal static bool ReadYes(this IReadWrite io, string format, float value) =>
|
internal static bool ReadYes(this IReadWrite io, string format, Number value) =>
|
||||||
io.ReadYes(string.Format(format, value));
|
io.ReadYes(string.Format(format, value));
|
||||||
internal static bool ReadYes(this IReadWrite io, string prompt) =>
|
internal static bool ReadYes(this IReadWrite io, string prompt) =>
|
||||||
io.ReadString(prompt).Equals("Yes", StringComparison.InvariantCultureIgnoreCase);
|
io.ReadString(prompt).Equals("Yes", StringComparison.InvariantCultureIgnoreCase);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ public static class Math
|
|||||||
{
|
{
|
||||||
public static float CalculateOriginal(float result) => (result + 1 - 5) * 5 / 8 * 5 - 3;
|
public static float CalculateOriginal(float result) => (result + 1 - 5) * 5 / 8 * 5 - 3;
|
||||||
|
|
||||||
public static string ShowWorking(float value) =>
|
public static string ShowWorking(Number value) =>
|
||||||
string.Format(
|
string.Format(
|
||||||
Formats.Working,
|
Formats.Working,
|
||||||
value,
|
value,
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
Bye!!!
|
||||||
|
|||||||
Reference in New Issue
Block a user