text format

Find and share HowTos to various installations / configurations!
2 posts • Page 1 of 1
gyu
Posts:11
Joined: Wed Mar 19, 2014 2:08 am

text format

Post by gyu »

Hi, Everyone

I hope everything is good for u.


Regarding the Text format,

I want to make a text format as " 1,234.56 ".

I can do it like "1234.56" using a text format, but can not make it "1,234.56" .

How to make it ?? Image
Attachments
text_format_1.png

leoknipp
Posts:2928
Joined: Tue Aug 24, 2010 7:28 pm

Re: text format

Post by leoknipp »

At the settings for a textfield this format cannot be set.
Also when using the CTRL function sprintf you cannot defined it as a format for the float value.

I tried to write a small script which converts a float variable into this format.
Attached you'll find an example code. I did just some short tests and I'm not sure if it works in all cases.
Even in this example the string result is not exactly the same as the float variable defined in the beginning caused by the fact that a float variable has a limited precision.

Maybe there are some other and better possibilities in CONTROL to get the same result.

main(mapping event)
{
float f_value, f_f1;
string s_value, s_part;

f_value = 1234567.890;

f_f1 = fmod(f_value,1);
DebugTN(__LINE__,"f_f1","f_f1");

sprintf(s_part,"%3d",f_f1*1000);
s_value = "." + s_part;

DebugTN(__LINE__,"s_value",s_value);
f_value = f_value - f_f1;

while(f_value > 1000)
{
DebugTN(__LINE__,"f_value",f_value);
f_value = f_value/1000;
DebugTN(__LINE__,"f_value",f_value);
f_f1 = fmod(f_value,1);
DebugTN(__LINE__,"f_f1",f_f1);

sprintf(s_part,"%3d",f_f1*1000);
DebugTN(__LINE__,"s_part",s_part);
s_value = "," + s_part + s_value;
DebugTN(__LINE__,"s_value",s_value);
}

sprintf(s_part,"%d",f_value);
s_value = s_part + s_value;
DebugTN(__LINE__,"s_value",s_value);

DebugTN(__LINE__,"s_value",s_value);
}

Best Regards
Leopold Knipp
Senior Support Specialist

2 posts • Page 1 of 1