X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 12 Jul 2007 10:38 AM by  anon
convert float/double to string
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
12 Jul 2007 10:38 AM
    Hello All: Is it possible to convert float/double data to string as the following format? 1.23e-5 float value -- > 123e-7 string 1.5e-5 float value -- > 15e-6 I want to select a range of my data for plotting and use the selected range as file name. Once I preserve the dot (.) , I may encounter some trouble. the file name may become filename_1.50e-5_2.50e-5.ps Thank you ! There are too many dots in the file name.

    Deleted User



    New Member


    Posts:
    New Member


    --
    12 Jul 2007 10:38 AM
    I am pretty sure the IDL's numeric string formatting commands do not allow a user to move the position of the decimal point from its position as the second non-blank character in the output string. Thus, one would have to create their own parser of IDL scientific format to decompose the IDL-formatted string, modify its subelements, and reassemble it in the format you desire. Below is a function with one algorithm that I think would work. It takes any real number as an argument and returns the format you are looking for: decimal-free coefficient and exponent with no leading zeroes. ; Call this with syntax: ; myNumberString = ELIMINATE_DOT_IN_SCIENTIFIC_FORMAT(myNumber) FUNCTION eliminate_dot_in_scientific_format, number ; Convert number to a scientifically formatted string sciFormattedString = string(number, FORMAT='(e0)') ; Split the scientific format string into three substrings, ; separated at the dot and the 'E' or 'e' splitString = strsplit(sciFormattedString, '[.e]', /REGEX, /EXTRACT) ; Get rid of excess 0's in the second element of 'splitString'. To do ; this we flip all the digits, let IDL's LONG() function parse out the ; unnecessary zeroes (which are leading zeroes after the flip), then ; flip the digits in the integer back to their original sequence. ; This requires multiple intermediate STRING() and BYTE() conversions. temp = string(reverse(byte(string( $ long(string(reverse(byte(splitString[1])))), FORMAT='(I0)')))) splitString[1] = temp ; Calculate how much the exponent value has to be incremented exponentSubtractor = strlen(splitString[1]) exponentMagnitude = fix(splitString[2]) exponentMagnitude -= exponentSubtractor ; decrement the value ; Make this into a string using the sign character of the third element newExponentString = string(exponentMagnitude, FORMAT='(I+0)') reformattedString = splitString[0] + splitString[1] + 'e' + newExponentString return, reformattedString END James Jones
    You are not authorized to post a reply.