# Convert a float into a string

**URL:** <https://discourse.julialang.org/t/convert-a-float-into-a-string/49513>\
**Category:** Numerics\
**Created:** [November 3, 2020, 10:01am UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513 "2020-11-03T10:01:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 10:01am UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/1 "2020-11-03T10:01:45Z")

</div>

Hi,

Is it possible to convert a float number into a string ?  
i.e. 32.234565 ()number into “32.234565” (string) or this works only with integers ?  
Thanks

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [November 3, 2020, 10:04am UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/2 "2020-11-03T10:04:54Z")

</div>

This works:

```julia
julia> string(rand()*100)
"61.47405081330051"

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 3, 2020, 11:05am UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/3 "2020-11-03T11:05:10Z")

</div>

The most common scenario is probably trying to write this somewhere. In that case, you probably want:

```julia
julia> x = rand()
0.23748752408015195

julia> println("This is $x in a string")
This is 0.23748752408015195 in a string

julia> using Printf

julia> println(@sprintf("This is %5.3f in a string, formatted by printf.",x))
This is 0.237 in a string, formatted by printf.

julia> string_x = @sprintf("%5.3f",x)
"0.237"

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 3, 2020, 12:31pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/4 "2020-11-03T12:31:02Z")

</div>

I would suggest only, that if you want to read it back with the exact precision, use `"%a"` instead, to output the floating point in hexadecimal notation.

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 5:13pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/5 "2020-11-03T17:13:58Z")

</div>

Thanks to all for your quick replies,

so…I finally came up with this to:

- 1 convert the float result to a string…and
- 2: extract the remainder numbers (while getting rid of the rest)

- the chop method would not accept a float but works with a string.

```julia
re=val/d
rf = string(re)
dotpos = findfirst(isequal('.'), rf)
rem = chop(rf, head = dotpos, tail = 0)

```

it works well that way although it seems a bit tedious on the cpu  
If anyone knows of a more efficient ways to get - just -  
the remainder value, drop a note please thanks.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [November 3, 2020, 5:31pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/6 "2020-11-03T17:31:02Z")

</div>

There’s probably a better way, but

```julia
julia> re = -100rand()
-66.8770948819094

julia> f1(re) = string(abs(re % 1))[3:end]
f1 (generic function with 1 method)

julia> function f2(re)
          rf = string(re)
          dotpos = findfirst(isequal('.'), rf)
          chop(rf, head = dotpos, tail = 0)
       end
f2 (generic function with 1 method)

julia> @btime f1($re)
  94.571 ns (3 allocations: 480 bytes)
"8770948819094002"

julia> @btime f2($re)
  269.911 ns (5 allocations: 528 bytes)
"8770948819094"

```

Answers don’t match exactly.

What do you need this for? Why are you converting numbers into strings?

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 5:33pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/7 "2020-11-03T17:33:49Z")

</div>

just trying to get long list of unique numbers, its okay for my needs if remainder lenght is not always of the same lenght.  
\*(investigating a remdiv function beside…)

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [November 3, 2020, 5:45pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/8 "2020-11-03T17:45:21Z")

</div>

Wouldn’t `rand(Int)` or `rand(Int128)` or `rand(-43:283342)` be good for this? And still, why strings?

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 5:50pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/9 "2020-11-03T17:50:04Z")

</div>

string is only coz the “chop” method did not work with float…else I ll stick to numbers…  
rem need to be based on some source values so rand is not an option in that case…

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 3, 2020, 6:15pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/10 "2020-11-03T18:15:54Z")

</div>

These source values cannot be passed as seed?

```julia
julia> using Random
julia> Random.seed!(1);
julia> rand(5)
5-element Array{Float64,1}:
 0.23603334566204692
 0.34651701419196046
 0.3127069683360675
 0.00790928339056074
 0.4886128300795012

julia> Random.seed!(1);
julia> rand(5)
5-element Array{Float64,1}:
 0.23603334566204692
 0.34651701419196046
 0.3127069683360675
 0.00790928339056074
 0.4886128300795012

```

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 6:19pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/11 "2020-11-03T18:19:15Z")

</div>

technicaly they probably can, but in this case, any random seed is not very usefull, nops ! 🙂

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 3, 2020, 6:28pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/12 "2020-11-03T18:28:31Z")

</div>

I really do not understand your use case.

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 6:34pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/13 "2020-11-03T18:34:22Z")

</div>

Thanks @Henrique_Becker, yes, that is some experiment out of the grid I guess - but dont worry - and thank you very much for trying to help in the topic anyway, I always learn many points from all the suggestions I can read from you guys here, its such a great - not to say unic - experience to be in this Julia forum anyway !  
🥇

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 3, 2020, 6:47pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/14 "2020-11-03T18:47:01Z")

</div>

> [@teriiju](#):
>
> rem need to be based on some source values

What are the source values?

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 6:49pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/15 "2020-11-03T18:49:41Z")

</div>

I think you call them seeds too…  
if c = b/a  
b and a are the seeds/sources here.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 3, 2020, 6:51pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/16 "2020-11-03T18:51:11Z")

</div>

But that’s just two numbers. You said you needed a long list of unique numbers. How does that connect with this?

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 6:51pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/17 "2020-11-03T18:51:56Z")

</div>

you just put it in a loop.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 3, 2020, 6:52pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/18 "2020-11-03T18:52:57Z")

</div>

That just creates the same number over and over.

You are really holding back the info here, huh? Can you give the big picture?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [November 3, 2020, 6:54pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/19 "2020-11-03T18:54:01Z")

</div>

I think they mean they use the fractional part to assemble new values…

---

<div class="post-metadata">

**Author:** ![teriiju](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teriiju/32/19029_2.png) [@teriiju](https://discourse.julialang.org/u/teriiju)\
**Post date:** [November 3, 2020, 6:55pm UTC](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513/20 "2020-11-03T18:55:22Z")

</div>

it would create the same result if you do not increase them at each loop. this is what needs to be done, its is very simple to do i=i+1  
change this at each loops, …nothing to hide here 🙂

[Next page](https://discourse.julialang.org/t/convert-a-float-into-a-string/49513.md?page=2)
