Correct formatting of numbers in output

This commit is contained in:
Andrew Cooper
2022-04-16 17:24:32 +10:00
parent 5fbf0dc37f
commit 8dd1180d87
7 changed files with 33 additions and 18 deletions

View File

@@ -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.

View File

@@ -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());

View 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} ";
}

View File

@@ -1,4 +1,3 @@
using Chief.Resources;
using static Chief.Resources.Resource;
namespace Chief;

View File

@@ -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);

View File

@@ -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,

View File

@@ -0,0 +1 @@
Bye!!!