1
0
forked from Alepha/Alepha
Files
Alepha/Utility/print_number.h

120 lines
2.3 KiB
C++

static_assert( __cplusplus > 2020'99 );
#pragma once
#include <Alepha/Alepha.h>
#include <cstdint>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
namespace Alepha::Hydrogen::Utility ::detail:: print_number_m
{
inline namespace exports
{
std::string formatPrint( std::uint64_t );
}
inline std::string
exports::formatPrint( std::uint64_t value )
{
const std::map< int, std::string > names
{
{ 1, "one" },
{ 2, "two" },
{ 3, "three" },
{ 4, "four" },
{ 5, "five" },
{ 6, "six" },
{ 7, "seven" },
{ 8, "eight" },
{ 9, "nine" },
{ 10, "ten" },
{ 11, "eleven" },
{ 12, "twelve" },
{ 13, "thirteen" },
{ 14, "fourteen" },
{ 15, "fifteen" },
{ 16, "sixteen" },
{ 17, "seventeen" },
{ 18, "eighteen" },
{ 19, "nineteen" },
};
const std::map< int, std::string > tens
{
{ 20, "twenty" },
{ 30, "thirty" },
{ 40, "forty" },
{ 50, "fifty" },
{ 60, "sixty" },
{ 70, "seventy" },
{ 80, "eighty" },
{ 90, "ninety" },
};
// One Thousand to the N
const std::map< std::uint64_t, std::string > powers
{
{ 0, "" },
{ 1, "thousand" },
{ 2, "million" },
{ 3, "billion" },
{ 4, "trillion" },
{ 5, "quadrillion" },
{ 6, "quintillion" },
};
std::vector< std::string > parts;
if( value == 0 ) return "zero";
for( int millennium= 0; value; ++millennium )
{
auto remainder= value % 1000;
value/= 1000;
if( not remainder ) continue;
std::string next;
if( remainder >= 100 ) next+= names.at( remainder / 100 ) + " hundred";
remainder%= 100;
if( remainder )
{
next+= " ";
if( remainder >= 20 )
{
next+= tens.at( remainder - remainder % 10 );
if( remainder % 10 ) next+= " " + names.at( remainder % 10 );
}
else
{
next+= names.at( remainder );
}
}
next+= " " + powers.at( millennium );
parts.push_back( std::move( next ) );
}
std::reverse( begin( parts ), end( parts ) );
std::string rv;
bool first= true;
for( auto part: parts )
{
while( part.back() == ' ' ) part.pop_back();
while( part.front() == ' ' ) part= part.substr( 1 );
if( not first ) rv+= ' ';
rv+= part;
first= false;
}
return rv;
}
}
namespace Alepha::Hydrogen::Utility::inline exports::inline print_number_m
{
using namespace detail::print_number_m::exports;
}