Convert a float into a string

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

This works:

julia> string(rand()*100)
"61.47405081330051"
2 Likes

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

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"

5 Likes

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.

3 Likes

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.
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.

There’s probably a better way, but

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?

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…)

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

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…

These source values cannot be passed as seed?

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

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

I really do not understand your use case.

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 !
:1st_place_medal:

What are the source values?

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

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

you just put it in a loop.

That just creates the same number over and over.

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

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

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 :slight_smile: