Jump to content

Number Type ( 50000 > 50.000 )


Recommended Posts

  • Honorable Member

You can use like this:
 

Spoiler

 

NUMBER_TEXT("123456789")

NUMBER_TEXT_INT(123456789)

 

Locale.hpp add;

	const char *locale_number(const char *string);
	const char *locale_number2(unsigned int mynumber);
#define NUMBER_TEXT(str) locale_number(str)
#define NUMBER_TEXT_INT(str) locale_number2(str)

Locale.cpp add;

#include <sstream>
#include <string.h>
using namespace std;
const char * locale_number(const char *string2)
{
	string str = string2; auto len = str.length(); auto counter = 2;
	while (counter < len) { if (counter % 3 == 0) str.insert(len - counter, ".");
		++counter;
	}
	return str.c_str();
}
const char * locale_number2(unsigned int mynumber)
{
	stringstream strr; strr << mynumber;
	return locale_number(strr.str().c_str());
}

TEST IN SERVER:

	auto gold = 10;
	ChatPacket(CHAT_TYPE_INFO, "We are testing(NUMBER_TEXT): %s", NUMBER_TEXT("123456789"));
	for (auto i : {10, 100, 1000, 10000, 500000}) {
		ch->ChatPacket(CHAT_TYPE_INFO, "We are testing(NUMBER_TEXT_INT): %s(i:%d)", NUMBER_TEXT_INT(gold*i), i);
	}

TEST:

#include <iostream>
#include <string.h>
using namespace std;
#define NUMBER_TEXT(str) locale_number(str)
#define NUMBER_TEXT_INT(str) locale_number2(str)
const char *locale_number(const char *string);
const char *locale_number2(int mynumber);
const char * locale_number(const char *string2)
{
	string str = string2;
	auto len = str.length();
	auto counter = 2;
	///////
	while (counter < len) { if (counter % 3 == 0) str.insert(len - counter, ".");
		++counter;
	}
	return str.c_str();
}
const char * locale_number2(int mynumber)
{
	auto str = std::to_string(mynumber);
	return locale_number(str.c_str());
}

int main ()
{
    auto gold = 10;
	for (auto i: {10, 100, 1000, 10000, 500000}) {
		cout << NUMBER_TEXT_INT(gold*i) << endl;
	}
	return 0;
}

Test result: EPRV2z.jpg

Edited by Metin2 Dev
Core X - External 2 Internal
  • Love 1

 

Link to comment
Share on other sites

	std::string NumberToMoneyString(long long n)
{
    std::string s;
    int cnt = 0;
    do
    {
        s.insert(0, 1, char('0' + n % 10));
        n /= 10;
	        if (++cnt == 3 && n)
        {
            s.insert(0, 1, '.');
            cnt = 0;
        }
    } while (n);
	    return s;
}
	

whose better?

Link to comment
Share on other sites

  • Honorable Member
2 minutes ago, hachiwari said:

 


	std::string NumberToMoneyString(long long n)
{
    std::string s;
    int cnt = 0;
    do
    {
        s.insert(0, 1, char('0' + n % 10));
        n /= 10;
	        if (++cnt == 3 && n)
        {
            s.insert(0, 1, '.');
            cnt = 0;
        }
    } while (n);
	    return s;
}
	

 

whose better?

My codes better, you can use with int and char. Also shorter than this

  • Love 1

 

Link to comment
Share on other sites

template<class T>
inline std::string NumberFormat(T value) 
{
	struct CustomNumpunct : std::numpunct < char >
	{
	protected:
		virtual char do_thousands_sep() const { return '.'; }
		virtual std::string do_grouping() const { return "\03"; }
	};

	std::stringstream ss;
	ss.imbue(std::locale(std::cout.getloc(), new CustomNumpunct));
	ss << std::fixed << value;
	return ss.str();
}
  // https://stackoverflow.com/questions/7276826/c-format-number-with-commas

 

  • Love 3
Link to comment
Share on other sites

  • 8 months later...
  • Honorable Member
On 11/16/2018 at 5:32 PM, Flourine said:

no.

self.OwnerMoney.SetText(localeInfo.NumberToMoneyString(str(exchange.GetElkFromSelf())))
self.TargetMoney.SetText(localeInfo.NumberToMoneyString(str(exchange.GetElkFromTarget())))

 

Link to comment
Share on other sites

  • 4 months later...
  • 2 weeks later...

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.