# Create filename from variables

**URL:** https://discourse.processing.org/t/create-filename-from-variables/14861
**Category:** Beginners
**Created:** [October 21, 2019, 6:55pm UTC](https://discourse.processing.org/t/create-filename-from-variables/14861 "2019-10-21T18:55:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)
#### Post date: [October 21, 2019, 6:55pm UTC](https://discourse.processing.org/t/create-filename-from-variables/14861/1 "2019-10-21T18:55:38Z")

</div>

I want to save an image from the display window using save().

For example:  
save(“filename.tif”);

I want, though, to create the filename using the values of some variables in my sketch.

so

int var1 = 290;  
float var2 = 2.000;  
float var3 = 8.000;

…some code

save(“290\_2.000\_8.000.tif”);

Is this possible?

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [October 21, 2019, 7:01pm UTC](https://discourse.processing.org/t/create-filename-from-variables/14861/2 "2019-10-21T19:01:32Z")

</div>

Yes, the filename is a String. Just convert any variable you want into a String, then stick the strings together and save to that:

```auto
String filename = str(var1) + "_" + str(var2) + "_" + str(var3) + ".tif";

save(filename);

```

Note that if you want to format the floats in specific ways – so that you don’t get 3.999999987 as part of your filename – you can use number formatting functions, for example: `nfs(var2, 0, 3);`

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [October 21, 2019, 7:23pm UTC](https://discourse.processing.org/t/create-filename-from-variables/14861/3 "2019-10-21T19:23:41Z")

</div>

Also just noting that, for the simple case of a sequence of numbered (not for your needs, but for others who may read this thread in the future):

`saveFrame()` also has a built-in method for numbering frames with the zero-padded frameCount:

```auto
saveFrame("line-######.png");
// Saves each frame as line-000001.png, line-000002.png, etc.

```

[https://processing.org/reference/saveFrame\_.html](https://processing.org/reference/saveFrame_.html)

---

<div class="post-metadata">

### Author: ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)
#### Post date: [October 21, 2019, 7:57pm UTC](https://discourse.processing.org/t/create-filename-from-variables/14861/4 "2019-10-21T19:57:51Z")

</div>

Thank you so much.  
On the use of nfs() and using your example,

float var2 = 3.999999987;

String vs = nfs(var2, 0, 3); // gives 4.000

The zero for the number of digits to the left of the decimal point does not seem to do anything. Is this left value more for putting leading zeros before some numbers so they align?

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [October 21, 2019, 8:57pm UTC](https://discourse.processing.org/t/create-filename-from-variables/14861/5 "2019-10-21T20:57:38Z")

</div>

> [@paulstgeorge](#):
>
> Is this left value more for putting leading zeros before some numbers so they align?

That is correct, it is for zero-padding. So, if you know your numbers will range from 0-64, and you want .0 precision, then:

String vs = nfs(var2, 2, 1);

Will give you outputs like 00.1, 01.2, or 12.3.

see:

> **[nfs() / Reference](https://processing.org/reference/nfs_.html)**
>
> Utility function for formatting numbers into strings. Similar to nf() but leaves a blank space in front of positive numbers, so they align with negative numbers in spite of the minus symbol. Th…
