The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Trim 0 and . from string
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

peter.cook

UK
356 Posts

Posted - 02/12/2004 :  10:18:37 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hi,

Can anyone help with :

I have a number as text from which I want to trim any right zeroes only if there is a decimal point but also decimal point if it is left eg

12.345 : 12.345
12.300 : 12.3
12.000 : 12
0.0010 : 0.001
123000 : 123000

Can anyone suggest slickest/quickest way thanks?

I've tried using .TrimRight('.') and .TrimRight('0') but I think I need to see there is a '.' in the string first..?

Cheers,

Pete


cpyang

USA
1406 Posts

Posted - 02/12/2004 :  12:17:41 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Simplest would be to use Origin's internal code which is used in LabTalk,

1. convert string into double
2. call DoubleToString to convert,
example



string str = "0.0010";
double val = atof(str);
char szTemp[30];
DoubleToStr(val, szTemp, 30, "*");
out_str(szTemp);



CP




Go to Top of Page

peter.cook

UK
356 Posts

Posted - 02/12/2004 :  12:30:14 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi CP,

I think the above returns eg 1e-3 whereas I actually want to see 0.001 as text as described.

Cheers,

Pete

Go to Top of Page

cpyang

USA
1406 Posts

Posted - 02/12/2004 :  12:58:13 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Sorry, you are right, default will be to convert to scientific notation at 1E-3, so the following code will do


void test(string strVal = "0.0010")
{
double dSLsave;
LT_get_var("@SL", &dSLsave);
LT_set_var("@SL", -6); // set Oriign's internal convert to scientific notation to 1E-6
double val = atof(strVal);
char szTemp[30];
DoubleToStr(val, szTemp, 30, "*");
LT_set_var("@SL", dSLsave); // restore after convert
out_str(szTemp);
}



CP


Edited by - cpyang on 02/12/2004 1:12:16 PM
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000