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

  • SEARCH
  • Home
  • 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 8198535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:58:19+00:00 2026-06-07T05:58:19+00:00

Hi Guys I have a text file that contains Amounts at Substring (34, 47)

  • 0

Hi Guys I have a text file that contains Amounts at Substring (34, 47) of each line. I need to sum Up all the Values to the End of the File. I have this code that I had started to build but I do not know how to proceed from here:

public class Addup {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws FileNotFoundException, IOException {
    // TODO code application logic here
    FileInputStream fs = new FileInputStream("C:/Analysis/RL004.TXT");
    BufferedReader br = new BufferedReader(new InputStreamReader(fs));
    String line;
    while((line = br.readLine()) != null){
        String num = line.substring(34, 47);

        double i = Double.parseDouble(num);
        System.out.println(i);
    }
}
}

The output is like this:

1.44576457E4
2.33434354E6
4.56875685E3

The Amount is in two decimal Places and I need the result also in the Two decimal Places. What Is the Best way to achieve this?

  • 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-07T05:58:20+00:00Added an answer on June 7, 2026 at 5:58 am

    DecimalFormat is the best option to use:

    double roundTwoDecimals(double d) {
                DecimalFormat twoDForm = new DecimalFormat("#.##");
            return Double.valueOf(twoDForm.format(d));
    }
    

    You can change your code to :

    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        double sum = 0.0;
        FileInputStream fs = new FileInputStream("C:/Analysis/RL004.TXT");
        BufferedReader br = new BufferedReader(new InputStreamReader(fs));
        String line;
        while((line = br.readLine()) != null){
            String num = line.substring(34, 47);
    
            double i = Double.parseDouble(num);
            sum = sum + i;
            DecimalFormat twoDForm = new DecimalFormat("#.##");
            System.out.println(Double.valueOf(twoDForm.format(i)));
        }
            System.out.println("SUM = " + Double.valueOf(twoDForm.format(sum)));
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Guys i have a text file in sdcard i need to read that file.
hi guys I have the following file format in hex were as each letter
Suppose I have a text file that says: This file has plain text Now,
I have several large text text files that all have the same structure and
Hey guys I've got a text file that a script creates (specifically dmidecode >
Hey guys, thanks for all the help that you can provide. I need a
I have the text file which contains 3 columns, first column having strings, second
guys i have arrays in which i have to match this kind of text
HI Guys I Have A jar File Named Column Reader. I am trying to
I have an AJAX script that post data in one of my PHP file:

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.