user3848630
user3848630

Reputation:

Replace the string

I'm working on my python script to add the numbers in the variable program_top.

I have the returns strings 315.0 which I want to replace it to 315.

When I use this:

program_top = 315 + 37.5 * idx

Here is the results:

20:48:02 T:3876  NOTICE: 315.0
20:48:02 T:3876  NOTICE: 315.0
20:48:02 T:3876  NOTICE: 315.0
20:48:02 T:3876  NOTICE: 315.0
20:48:02 T:3876  NOTICE: 315.0
20:48:02 T:3876  NOTICE: 315.0
20:48:02 T:3876  NOTICE: 315.0
20:48:02 T:3876  NOTICE: 315.0
20:48:02 T:3876  NOTICE: 315.0
20:48:02 T:3876  NOTICE: 315.0
20:48:02 T:3876  NOTICE: 315.0

I want to make the results like this:

20:48:02 T:3876  NOTICE: 315
20:48:02 T:3876  NOTICE: 315
20:48:02 T:3876  NOTICE: 315
20:48:02 T:3876  NOTICE: 315
20:48:02 T:3876  NOTICE: 315
20:48:02 T:3876  NOTICE: 315
20:48:02 T:3876  NOTICE: 315
20:48:02 T:3876  NOTICE: 315
20:48:02 T:3876  NOTICE: 315
20:48:02 T:3876  NOTICE: 315
20:48:02 T:3876  NOTICE: 315

Can you please tell me how I can use program_top = 315 + 37.5 * idx to remove the .0 to replace it with empty string?

Upvotes: 0

Views: 65

Answers (1)

matsjoyce
matsjoyce

Reputation: 5844

Use program_top = int(315 + 37.5 * idx). This converts the float to an integer, so there will be no decimal places. If you only want to remove the decimal places if it ends with .0, try:

program_top = 315 + 37.5 * idx
if not program_top % 1.0:
    program_top = int(program_top)

String method:

program_top = str(315 + 37.5 * idx)
program_top = program_top[:-2] if program_top.endswith(".0") else program_top

However, the number based solution (above) is better.

Upvotes: 3

Related Questions