1

Is there any way to split a number into its integer and decimal parts?

I've tried the int() "function":

int(5.5) = 5.

But I've got no idea how do I get the decimal?

Example:

(I have)

a = 12.34

(I want)

b = 12
c = 34

a = 12.34

b = int(a)

c = ????

Thanks!!!!

(Update)

My specific problem, here we have (thanks again for the support)

I have a set of data, with different blocks. For each block, I want to make the same plot. The title of the plot and the name of the output png depends on the block.

With this goal in mind I've created a loop

do for [j=0:int(A_blocks-2)]{

i=0 + 0.4*j

set output 'Mz_NMcs5000_Hext'.i.'_JC1_JSn05_JIntn05_R11_tSh2.png'

set title "H = '.i.'  J_C = 1  J_S = J_{Int} = - 0.5"

plot filename index j using 1:5 w lp pt 5 lt rgb "black" title "Mag_T", "" index j u 1:20  w lp pt 9 lt rgb "red" title "Mag_{Int-S}"
}

The problem I have is that I only can concatenate using the dot if the value is an integer. I get this error:

internal error : STRING operator applied to undefined or non-STRING variable

5
  • Welcome to StackOverflow! Well, adding unrelated code doesn't help not getting rejected. If the question is clear and neither a duplicate nor obvious to answer by a simple web search you will get an answer. Should 1.23 return 23, and 1.230 return 230? What do you want to do with the result? Commented Apr 9, 2021 at 13:16
  • Thanks! I'm new with this and still trying to guess how it works Commented Apr 9, 2021 at 13:25
  • that's fine. So, do you want 23 or 230 in case of 1.230? What do you want to get for example for 5.001... 1? and what do you want to get for 5.01 and 5.1? 1 as well? I doubt. Please clarify. Commented Apr 9, 2021 at 13:39
  • ok. this looks like a xy-problem, see meta.stackexchange.com/questions/66377/what-is-the-xy-problem What do you actually want to achieve? Including a float number into a filename and a title? Commented Apr 9, 2021 at 14:14
  • Yes, I want to include a float number into a filename and title. Exactly that! Commented Apr 9, 2021 at 14:19

2 Answers 2

1

If you want to include a float number into a title or a file name use the string formatting via sprintf(), check help sprintf and help format specifiers.

a = 1.234
myFile = sprintf("MyFileName_%g_MoreParameters.png",a)
print myFile

Result:

MyFileName_1.234_MoreParameters.png
Sign up to request clarification or add additional context in comments.

Comments

1

Your question seems to have shifted, but the answer to the original question as asked is

gnuplot> a = 5.5555555555555
gnuplot> b = a - floor(a)
gnuplot> print b
0.5555555555555

3 Comments

Thanks!!! And is there a way to convert 0.555555555 into an integer 555555555 ?
That is not a uniquely-defined operation. How many decimal places of precision to keep? Round up or round down?
Well, I guess that for 0.123 ---> 123 and for 1.203500 ---> 2035. Now, it isn't a big deal, cause I need it for the title and output file name. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.