Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Numeric Edit/Masking Routine

Status
Not open for further replies.

TomKattt

MIS
Sep 18, 2003
2
US
Hello, I hope someone can help. I need a routine to convert a number (a float, in this case) to an edited string. The format conversions available in C are not adequate. I would like to end up with numbers that are comma-differentiated.
Example: -123,456,789.12

The closes I can come to this is to sscanf the floating number into a string, which gives me -123456789.12.
 
this is a pseudocode:
you hage got for example this:
xx = "-123456789.12";

you will
n = strlen(xx);
for(i = n - 1; i >= 0; i++)
{
if(xx == '-') break;
if(xx = '.')
{
foudPoint = 1;
pointPosition = i;
}
if(foundPoint)
{
if(i != pointPosition)
{
if(!((i - pointPosition - 1) % 3))
{
move all characters to right and insert there
a ','
}
}
}
}

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Thanks for your help. Your routine is simpler than mine and looks like it will work better. I'll give it a shot. Although I need to allow for + signs or no signs but that should not be hard to add to this.
 
you can change:
if(xx == '-' || xx == '+') break;


Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Code:
sorry of above, [i] goes in italic font.
if(xx[i] == '-' || xx[i] == '+') break;

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top