Read Mode

Float Function is working in CMD, But Not in Batch File | CMD FAQs

Got a mail from Mr. Lilián luna de Valencia telling us about a Problem he's facing in the usage of Float Function ver.1.0 in his batch file. As while demonstrating and creating this file #TheBATeam was only concerned about the output on the console screen, so that's why we didn't consider this phase of the usage of file... We really appreciate your feedback to let us know about this problem. 

Moreover we also came to know about the another problem that it only solves the single operation at once. We will add Multiple operations solving and returning the result to a Variable like features in ver.2.0 of this Function. But Meanwhile, for you sake of usage and project completion. we gonna tell you a simple trick that you can easily do in your batch file, in order to complete your goal of saving output of float function to a variable.

This trick will not only allow you to read and save the output of the function indirectly to the variable, But also very instructive for the knowledge of basic data fetching in any case you require. You can manipulate commands for your usage purpose. Combine multiple commands to make this happen. You'll understand the concept of throwing one command's output to other command as input and then manipulating as you need.

Related Post: WTF Diffrence between %Var% and !Var! in Batch??

The following lines of code will help you, just add them to your batch file. Where-ever you need to save output to a variable, you need to paste it there. Better If you can use it inside a loop, then you'll no need to paste the same code multiple times and it saves space too.

Algorithm of working of code:

Suppose you have to save value of Pi to a variable named 'result'
  1. Firstly send the output of float function to a file...e.g. 'a.txt'
  2. Then read the output and assign the value to 'result'
  3. Delete the 'a.txt' file...
Float Function is working in CMD, But Not in Batch File | CMD FAQs

float 355 / 113 > a.txt
for /f "tokens=*" %%A in (a.txt) do set result=%%A
del /q "a.txt"
echo. %result%

Note: (Text-Selection is Disabled!)
No need of Copy-pasting the code above, Its there for the sake of understanding the concept. We've provided the download link below with the example of usage of this trick. We hope it will help you out.

If you are trying the above code directly into the cmd console... Use '%A' instead of '%%A' in above code. Because, There is a Difference between %%A and %%a, as 'for loop' in batch is case sensitive.
Your requirement will be taken under consideration while launching the Ver.2.0 of float function.
Float Function is working in CMD, But Not in Batch File | CMD FAQs

Although, Keeping the Mess this extra code will create in your main project file - in mind. We've already launched the Float Function v.1.1 | By Kvc, For the sake of making beautiful batch projects. This new version is based on this request and feedback, and allows the programmer to save the Result along with the calculation call. You can check it out.

"Learn from yesterday, live for today, hope for tomorrow.
The important thing is not to stop questioning."

If you are still in doubt, or want to give suggestions for the new version of this Float Function. You are welcome for making a simple comment below in the comment field. It will take 2 min. of your time, to contribute to something beautiful for happening. I'll wait for your comments. Thanks for your time. 😊

Keep Sharing, Keep Learning...
Be Happy, Spread Happiness...

Download Link: 


Also Read: Float Function v.1.1 | By Kvc


  1. this problem have i whith box.bat :D

    1. Yeah, All you have to do is - change the command syntax a bit for using in batch files. You need to 'Call' the functions from batch program - For proper functioning.

      Syntax: Call [Function_Name] [Parameters]
      E.g. Call Float 355 / 113
      E.g. Call Box 5 5 5 5

      Hope, it will clear your doubts!

  2. I am afraid I don't understand the use of for command here. If I want to use for why should I echo the output to another file instead of using builtin method like:-
    for /f %%a in ('float 355 / 113') do set result=%%a
    Or if I'm already out-parsing to another file then this method may be useful:-
    float 355 / 113 >a.txt
    set /p result=<a.txt
    Hope that helpes.


    1. Exactly! You can use these methods too. But, If you'll notice - We've republished this article from 2014. Because, it was a page of the website instead of being an article. Although, We've alredy solved this little problem in Float Function v.1.1. We've used the Tunneling method to save the value of the variable directly into the variable specified by the user.

      If you want to use, it wil be better to use, v.1.1 than v.1.0. :)


Powered by Blogger.