mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-12 07:40:50 -08:00
Correct formatting of numbers in output
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Games.Common.Numbers;
|
||||
|
||||
namespace Games.Common.IO;
|
||||
|
||||
@@ -76,17 +75,16 @@ public interface IReadWrite
|
||||
void WriteLine(string message = "");
|
||||
|
||||
/// <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>
|
||||
/// <param name="value">The <see cref="float" /> to be written.</param>
|
||||
void Write(float value);
|
||||
/// <param name="value">The <see cref="Number" /> to be written.</param>
|
||||
void Write(Number value);
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="float" /> to output, formatted per the BASIC interpreter, with leading and trailing spaces,
|
||||
/// followed by a new-line.
|
||||
/// Writes a <see cref="Number" /> to output.
|
||||
/// </summary>
|
||||
/// <param name="value">The <see cref="float" /> to be written.</param>
|
||||
void WriteLine(float value);
|
||||
/// <param name="value">The <see cref="Number" /> to be written.</param>
|
||||
void WriteLine(Number value);
|
||||
|
||||
/// <summary>
|
||||
/// Writes an <see cref="object" /> to output.
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Games.Common.Numbers;
|
||||
|
||||
namespace Games.Common.IO;
|
||||
|
||||
@@ -100,9 +97,9 @@ public class TextIO : IReadWrite
|
||||
|
||||
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());
|
||||
|
||||
|
||||
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;
|
||||
|
||||
namespace Chief;
|
||||
|
||||
@@ -2,7 +2,7 @@ namespace Chief;
|
||||
|
||||
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));
|
||||
internal static bool ReadYes(this IReadWrite io, string prompt) =>
|
||||
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 string ShowWorking(float value) =>
|
||||
public static string ShowWorking(Number value) =>
|
||||
string.Format(
|
||||
Formats.Working,
|
||||
value,
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Bye!!!
|
||||
|
||||
Reference in New Issue
Block a user