Convert float to formatted wxString Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
nime
Experienced Solver
Experienced Solver
Posts: 74
Joined: Sat Mar 13, 2010 5:12 pm

Convert float to formatted wxString

Post by nime »

Hello (I said I'll be back) :oops:

I cant find a handy way (without writing code) for converting float number with wx functions to formatted wxString as was in VB6 format (j, "##0.00") or so and older Basics with PRINT USING "##,###,###.##" .

for example:
float j = 1254.325245;
wxString jk = wxString::Format(wxT("%f"),j);
With this code I can get jk as string representation of j like "1254,325245".
I would like to get " 1.254,33", together with leading spaces which varies depending on number.

And second:
How can I get systems decimal separator sign and thousands separator sign?
TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Post by TrV »

Code: Select all

wxString::Format(wxT("%.2f"), j);
More information: http://www.cplusplus.com/reference/clib ... io/printf/

AFAIK, there is no simple way to display thousand separators.
nime
Experienced Solver
Experienced Solver
Posts: 74
Joined: Sat Mar 13, 2010 5:12 pm

Post by nime »

Yes I see, this works for printf.
Can I get it to string?

Like this:

Code: Select all

 wxString jk = wxString::Format(wxT("%10d %.2f"),j);
I get weird (incorrect) result...
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE »

nime wrote:

Code: Select all

 wxString jk = wxString::Format(wxT("%10d %.2f"),j);
I get weird (incorrect) result...
Sure.
In your case wxString::Format() expects a decimal and a float, but you only pass in "j" as the decimal. The float is missing, hence the "unexpected", yet to be expected, behavior.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Post by TrV »

As upCASE says:

Code: Select all

wxString jk = wxString::Format(wxT("%.2f"), j);
and

Code: Select all

wxString jk = wxString::Format(wxT("%10d %.2f"), k, j);
work.

Code: Select all

wxString jk = wxString::Format(wxT("%10d %.2f"), j);
is incorrect.
nime
Experienced Solver
Experienced Solver
Posts: 74
Joined: Sat Mar 13, 2010 5:12 pm

Post by nime »

Hm,
Yes it works with little issue.

Code: Select all

float j = .325245;
int k = 1254;
wxString jk = wxString::Format(wxT("%10d%.2f"), k, j);
I get result 12540,33 instead of 1254.33
Btw, how to easy crash number to decimal and float?
In Basic I will do :
l=1254.325245
k=FIX(l)
j=l-k

Now I need some time to "cache" this and read about.
Seems that is everything possible to get with this.
Actually, only thousands (and may be million) characters missing.
Is here also some provided trick?
I didnt find nothing useful regarding this reading about printf.
TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Post by TrV »

nime wrote:Hm,
Yes it works with little issue.

Code: Select all

float j = .325245;
int k = 1254;
wxString jk = wxString::Format(wxT("%10d%.2f"), k, j);
I get result 12540,33 instead of 1254.33
Is the problem about , (comma) or . (dot) ?
Otherwise, ".33" is not a number. "0.33" is.
If you really need to "erase" the "0", use wxString methods.

nime wrote:Btw, how to easy crash number to decimal and float?
In Basic I will do :
l=1254.325245
k=FIX(l)
j=l-k

Now I need some time to "cache" this and read about.
Seems that is everything possible to get with this.
"crash" numbers???
Please precise what type is l, k and j, what is FIX() and what you want to achieve.
VB is not a typed programming language. C++ is.

nime wrote:Actually, only thousands (and may be million) characters missing.
Is here also some provided trick?
I didnt find nothing useful regarding this reading about printf.
I've already answered.
nime
Experienced Solver
Experienced Solver
Posts: 74
Joined: Sat Mar 13, 2010 5:12 pm

Post by nime »

Decimal point was typo.

l, k and j can be single, long or double.
Depends how is declared in scope.

Dim l as long, k as long, j as long
l=1254.325245
k=FIX(l) 'result = 1254
j=l-k 'result = 0.325245 (Basic allow and .325245)

I am not sure what is typed language but if this is types of data that is in Basic also important. Othervise errors occured. Without knowing them you cant work.

I still didnt catch how I can get my favourite 1.254,33 from 1254.325245 cause we come to 12540,33.
But you give me enough important informations.
I will try now to finish alone.

Thanks to all which helps.
TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Post by TrV »

nime wrote: Dim l as long, k as long, j as long
l=1254.325245
k=FIX(l) 'result = 1254
j=l-k 'result = 0.325245 (Basic allow and .325245)
"l" cannot definitely be "long" in C++ because "long" is a shortcut for "long integer".

In C++ one has to write:

Code: Select all

double l=1254.325245;
int k = (int)l; // decimal part is removed
double j = l - k;
nime wrote:Hm,
Yes it works with little issue.

Code: Select all

float j = .325245;
int k = 1254;
wxString jk = wxString::Format(wxT("%10d%.2f"), k, j);
I get result 12540,33 instead of 1254.33
nime wrote:I still didnt catch how I can get my favourite 1.254,33 from 1254.325245 cause we come to 12540,33.
How can you expect to have one number whereas you're using 2??? It didn't make any sense to me.

If the idea is to have separated integer and decimal parts of one floating point number and recombine them later, then just add integer and decimal parts to make one and only one number.

Code: Select all

float j = .325245;
int k = 1254;
wxString jk = wxString::Format(wxT("%.2f"), k + j);
nime
Experienced Solver
Experienced Solver
Posts: 74
Joined: Sat Mar 13, 2010 5:12 pm

Post by nime »

OK, TrV, thanks a lot.
int k = (int)l we write like Dim k as integer: k = cint(l), FIX is another story.

Now I simply have to read and try various situations with formatting. I am not sure that I know explain what I need at start but we comme wery close. Printf will be also useful for printing to files ko keep numers aligned with fixed fonts.

I like those C++ every day double than yesterday what mean that I advancing with big steps. :lol: Because my level of knowledge is not hard multiplying with 2.

And all of this is extremelly interesting for me.
TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Post by TrV »

Code: Select all

int k = (int)l;
() is cast operator. It's a very important operator in C++ (and OOP in general).

I would strongly advise you to learn C basics, then C++ basics. These things are really important to know and master before doing things more complex.
nime
Experienced Solver
Experienced Solver
Posts: 74
Joined: Sat Mar 13, 2010 5:12 pm

Post by nime »

Yes, you are right.
Thats exactly what I'm trying to do.
I read much these days.
Post Reply