Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:08:23+00:00 2026-06-14T21:08:23+00:00

I have some data here in a text file which I have use regular

  • 0

I have some data here in a text file which I have use regular expression to parse the data
from the file and store the number and index in two variables. I would like to rounded the numbers to same decimal place and write it into a new file (with it’s index A,B,C)

This set of data is all in 3 decimal place

A 11,000.233

B 4.000

C 14,798.241

Another set of data is in 2 decimal place

A 11,000.23

B 4.00

C 14,798.24

They are all in string format initially when I read them using readlines()

But when I convert the string in the first set of data to float and then round it from 3 decimal place to 2 decimal place by using str(round(float(number),2))

I have encountered two problems

  1. It will give me an ValueError for A (11,000.233) and C (14,798.241)

    I understand it is caused by the , in 11,000.23. But I don’t know how to deal with it.Could anyone give me some suggestion on how to deal with it?

  2. For B, when I try to convert the numbers with the same method, it give me 4.0 instead of 4.00. Is there anyway that can output 4.00 instead of 4.0?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:08:25+00:00Added an answer on June 14, 2026 at 9:08 pm

    split the string at "," and use "".join():

    In [32]: strs="11,000.233"
    
    In [33]: float("".join(strs.split(",")))
    Out[33]: 11000.233
    
    In [34]: strs="14,798.24"
    
    In [35]: float("".join(strs.split(",")))
    Out[35]: 14798.24
    

    you can use string formatting to get 4.00:

    In [36]: strs="4.00"
    
    In [37]: float(strs)
    Out[37]: 4.0
    
    In [39]: format(float(strs),".2f")
    Out[39]: '4.00'
    
    In [40]: "{0:.2f}".format(float(strs))
    Out[40]: '4.00'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file which was created using some Microsoft reporting tool. The
I am trying to use regex to parse out some lines from text read
I have a simple layout file which displays the icons and some text view
I have some troubles with receiving data on server side. Here goes the code:
Here is the scenario: I have a visual that displays some data The data
I have a MySQL/Rails app that needs search. Here's some info about the data:
I have some data from log files and would like to group entries by
I have some data which (quite reasonably) uses null and false for different meanings.
I have some data and need to create a json file with this structure
I have some data which i need to make some statistics. I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.