Jump to content

Recommended Posts

Posted

I have a text file which has over 1000 lines, and I wish to replace one particular - I was able to do the replacement - but I have a issue - I want the numbers to be of specific format 

Eg; my command was as follows, 

my command >>>>> _filewritetoline($inp_file,$inp_replacement,"WAVE1.00STOK"&$hdet&"      "&$tasso&"        "&"270.0      D          5.0  72MS     1",true)

my output >>>>>>       WAVE1.00STOK10.06        9.800         270.0      D          5.0  72MS     1

how do I make sure that the $hdet=10.06 is printed as 10.060(3 digits after decimel) and same with $tasso and so on.. also, how to maintain the required gap between the variables - is it by manually putting spaces ?

 

thanks guys

Posted

Thanks Subz, I was not precise with my requirement.... forgot to mention that the criteria was that numbers should always occupy 6 spaces ie; 10.060 is fine, if the variable $hdet was 9.060, it should appear as 09.060... also, if the number was 100.061 or 100.066, it should appear as 100.06 and 100.07 respectively.  thanks

Posted

You mean something like this?

MsgBox(0,'', _FormatNumber($tasso))
Func _FormatNumber($iNumber)
    Local $iLength = StringLen(StringLeft($iNumber, StringInStr($iNumber, ".") - 1))
    Switch $iLength
        Case 6
            Return StringFormat("%06", $iNumber)
        Case 5
            Return StringFormat("%05", $iNumber)
        Case 4
            Return StringFormat("%04.1f", $iNumber)
        Case Else
            Return StringFormat("%03.2f", $iNumber)
    EndSwitch
EndFunc

 

Posted

Another runnable example.

;#include <File.au3>

Local $hdet = 10.061, $tasso = 9.061

Local $sLine = StringFormat("WAVE1.00STOK%06s      %06s        270.0      D          5.0  72MS     1", _
        _FormatNumber($hdet), _FormatNumber($tasso))
ConsoleWrite($sLine & @CRLF)

Local $sLine2 = StringFormat("WAVE1.00STOK%06s\t%06s        270.0      D%10s5.0  72MS     1", _
        _FormatNumber($hdet), _FormatNumber($tasso), "")
ConsoleWrite($sLine2 & @CRLF)

;_FileWriteToLine($inp_file, $inp_replacement, $sLine, True)

;#cs ; -------_FormatNumber() Test Data -------
ConsoleWrite(_FormatNumber(1234.061) & @CRLF)
ConsoleWrite(_FormatNumber(100.061) & @CRLF)
ConsoleWrite(_FormatNumber(100.068) & @CRLF)
ConsoleWrite(StringFormat("%06s", _FormatNumber(9.061)) & @CRLF) ; "%06s" adds leading zeros to make up a 6 character string.
ConsoleWrite(_FormatNumber(10.0616) & @CRLF)
;#ce

Func _FormatNumber($iNumber)
    Local $iLength = StringLen(Int($iNumber))
    Switch $iLength
        Case 4
            Return StringFormat("%4.1f", $iNumber)
        Case 3
            Return StringFormat("%3.2f", $iNumber)
        Case 2
            Return StringFormat("%2.3f", $iNumber)
        Case 1
            Return StringFormat("%1.3f", $iNumber)
    EndSwitch
EndFunc   ;==>_FormatNumber

 

Posted (edited)

thanks Subz and Malkey... that helped me

 

What if  the variables are in format $hdet=10.061E6, and I want the total input spaces to be 6, so the results required is 10.1E6

Edited by rkr

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...