need help on some converting command


 
Thread Tools Search this Thread
Top Forums Programming need help on some converting command
# 1  
Old 07-21-2005
Java need help on some converting command

Hi all,
i am trying to use C in unix platform to convert a double variable (floating point) to a string variable.

i tried using sprintf, ecvt, fcvt,gcvt.
but all gave me funny output or altered the content.

does anyone know how to convert the data and keep the original content.

for example, 3.3 or 3.33e2 in double to string

if anyone knows the ans, plas kindly post an example of the code.

thanks a lot.
# 2  
Old 07-21-2005
This worked just fine for me:

Code:
#include<stdio.h>

int main() {
        double a=7.123451;
        char b[10];
        sprintf(b,"%lf",a);
        printf("%s\n",b);
}

[edit]
It's been a while since I did this sort of coding, so if there is anything wrong in the syntax, please point it out.
[/edit]

Last edited by blowtorch; 07-21-2005 at 06:37 AM.. Reason: to add a note
# 3  
Old 07-22-2005
Computer

Quote:
Originally Posted by blowtorch
This worked just fine for me:

Code:
#include<stdio.h>

int main() {
        double a=7.123451;
        char b[10];
        sprintf(b,"%lf",a);
        printf("%s\n",b);
}

[edit]
It's been a while since I did this sort of coding, so if there is anything wrong in the syntax, please point it out.
[/edit]
thanks for the reply.

but i am getting input from another function which i do not know what the content will be. for example, it may be 7.123451 or 7.1234e5.
so for sprintf, i will need to specify it is %f or %e.
do you know any function that do not have to specify the type , rather convert any floating point value.

thanks for helping
# 4  
Old 07-22-2005
"%lf" works with both "%e" and "%f"
Code:
#include<stdio.h>

int main() {
        double a=7.1234e5,c=7.123451;
        char b[50],d[50];
        sprintf(b,"%lf",a);
        sprintf(d,"%lf",c);
        printf("b:%s\td:%s\n",b,d);
}

Can your input be anything else apart from what you've said it could be?

Also, moderators, can this be moved to the C programming section?

Last edited by blowtorch; 07-22-2005 at 05:52 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help in converting

I have Excel file with the below three columns, i need your expertise in converting this to .csv file delimiter "|" Excel - Serial Number Serial Name Serial Brand 111 test sample 123 test2 sample1 134 ... (9 Replies)
Discussion started by: kiran_hp
9 Replies

2. UNIX for Advanced & Expert Users

Converting xls file to xlsx on UNIX script / command line.

Hi All, Am needing advise on how to convert xls file to xlsx format on Solaris unix command line or scripting. I tried searching online but it looks like I need to either use Perl packages of Excel or Python packages or some other 3rd party tool. Problem is to install any of these will require... (2 Replies)
Discussion started by: arvindshukla81
2 Replies

3. Shell Programming and Scripting

Using loop command in UNIX for converting column to row

Dear folks I have 300 files which one of them are looking like: 1.SNP 0 0 1 0 I am looking for desire output: 1.SNP 0 0 1 0 I used this below command to run all of the 300 file at the same time for file in *.SNP; do awk '{printf( "%s ", $1 );} END {printf("\n");}' $file >... (1 Reply)
Discussion started by: sajmar
1 Replies

4. Shell Programming and Scripting

converting empty value into NA

I have a set of values separated by a tab ch:12 1 3 4 ch:13 3 3 4 ch:25 1 8 ch:23 2 8 1 There is a missing value in the third column and i would like replace it with NA such that the final output would look like ch:12 1 3 ... (7 Replies)
Discussion started by: johnkim0806
7 Replies

5. Shell Programming and Scripting

Converting a list to X columns of csv (& wrapping a command around it)

Hi All, Random question, how would you convert a data file from a list like so: 12345 12346 12347 12348 12349 12350 ... <snip 100+ lines> ... to comma separated X columns across: 12345,12346,12347 12348,12349,12350 Why would you want to do this? The background to this is a... (2 Replies)
Discussion started by: craigp84
2 Replies

6. Shell Programming and Scripting

command for converting 4 column data to 1 column

dear friends I want to convert four column data to one column data. For example: from 1, 2, 3, 4 5, 6, 7, 8to 1 2 3 4 5 6 7 8what is the general command for that type of convertion. thanks (5 Replies)
Discussion started by: rpf
5 Replies

7. Shell Programming and Scripting

Converting \0 to a \n

Hi - I have seen some similar posts but I am a bit stumped here below is the first line of a 'od -c filename' command. I want to change the \0 to \n 0000000 l s \0 c d - \0 c d . . \0 l s I have tried a sed construct in a script......... sed... (2 Replies)
Discussion started by: ajcannon
2 Replies

8. SCO

Converting

I use Sco_Sv 3.2v5.0.5 with parellel conection using dump terminals and i want to convert them to desktop pc. Anybody knows what hardware and other thing that would be involved? (3 Replies)
Discussion started by: seeyou
3 Replies

9. Shell Programming and Scripting

command for converting string to integer

Hi ... I am trying to calculate the time needed for a command to execute.. but the resulting value is getting as string.. so i am not able to use "expr " command.. please help me to convert the value to integer so that i can proceed with my script.. Regards esham (1 Reply)
Discussion started by: esham
1 Replies

10. UNIX for Dummies Questions & Answers

converting kb to mb

When I create filesystems in AIX i often get confused(using smit) When you specify size in aix, it is asked like this SIZE of file system (in 512-byte blocks) I never seem to grasp this, what is the equation to get say 500mb? Or is there a program anyone knows of that does this, like a... (1 Reply)
Discussion started by: csaunders
1 Replies
Login or Register to Ask a Question