mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-19 08:11:14 -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} ";
|
||||
}
|
||||
Reference in New Issue
Block a user