Java number formatting: Locale

The tutor continues about number formatting with Java.

We don’t live in countries any more; rather,  we live in the world.  Right now, for instance, my family is off in the US.  I’m not with them; I stayed home to run the business.  In fact, I’m not a traveller.

I learn about the world from print, which is how  I recently became aware that number formats vary from country to country.  Behold the many faces of 1 139 275.78:

France 1 139 275,78
Germany 1.139.275,78
US, Canada, English 1,139,275.78
Canada (French) 1 139 275,78

Notice that none of those nation-specific formats is the way I write the number (once again: 1 139 275.78). I was taught that, in Metric, the thousands separators are spaces, not commas. Yet, being in an English-speaking country, I use a decimal point rather than a comma. It’s an interesting mash of formats that I believe scientifically-educated North Americans commonly use:)

Being an international programming language, Java is prepared to accommodate the number formats used in different countries. Java calls a country a locale.

I wrote two earlier posts on Java decimal number formatting here and here. Any Java programmer who is reading this article likely wants the key lines that enable a program to translate a number from one country’s format to another’s. You start with an instance of NumberFormat, setting it with the desired locale:

NumberFormat numform = NumberFormat.getInstance(Locale.COUNTRY)

For example, to get the format used in France:

NumberFormat numform0 = NumberFormat.getInstance(Locale.FRANCE);

Next, you cast the NumberFormat instance into a DecimalFormat object:

DecimalFormat decform0 = (DecimalFormat)numform0;

Finally, you apply the format descriptor string; for example,


Now, you can format number num0 to String string0, in the style of France:

string0 = decform0.format(num0);

To use the above code effectively, you’ll need to include the lines

import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.util.Locale;

above the class definition.

As far as I’ve read, not necessarily every country has a locale in Java. You can see which ones do here.


Other sources:

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Java: decimal formatting, continued

The tutor continues with the potentially tricky topic of Java decimal formatting.

With yesterday’s post I broke the ice about Java DecimalFormat with a short demonstration program. Now, some key points of explanation:

  1. The format descriptor string “#,###.00” means that two decimal places will always be shown, even if they are zeros. However, the number 0.79 will display as .79 (no zero in front of the decimal point). A “#” means that the place will not be shown if it is a leading or trailing zero, whereas a “0” means the place will be shown, with a zero if it has no other value. Therefore, the number 2, formatted as “#0,000.00”, will be rendered as 0,002.00 by DecimalFormat.
  2. Notice that the output of DecimalFormat is a string (that looks like a number).
  3. The Scanner is not intrinsically connected to DecimalFormat; it is used in the program to read the numbers from the command line.

While this probably covers the program from yesterday, there is still more to say about Java’s DecimalFormat. Not surprisingly, I’ll be continuing about it in future posts:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Java: formatting decimal numbers, part 0

We’re getting into a serious topic. The tutor rolls up his sleeves.

In math, formatting rarely matters.  If the correct answer is 0.012, you can answer as 1.2e-2, 3/250, or 0.01200. Most computer environments, like most calculators, give an “unformatted” answer to a calculation. Not too surprisingly, when I ask Java for the square root of 14, it gives 3.7416573867739413, which is perhaps more than sufficient.

In physics, chemistry, or financial contexts, people aren’t as tolerant of unformatted answers. Scientists expect answers to correct precision. Monetary values are expected to two decimal places.

Java has a class that helps the programmer to display numeric output according to any desired format: DecimalFormat. It’s invoked with a pattern that describes the desired format. An example:

DecimalFormat form0 = new DecimalFormat(“#,###.00”);

In order to use the DecimalFormat class, you must include the line

import java.text.DecimalFormat;

above the class declaration.

Following is a short program that illustrates the use of Java’s DecimalFormat. (It uses the Java Scanner, about which I wrote a series starting here.)

import java.text.DecimalFormat;
import java.util.Scanner;//needed for the scanner

class DecFormatEx{
/*This class reads numbers given with the command call, then displays them formatted to two decimal places with the thousands separated by commas.*/
public static void main(String[] args){

DecimalFormat dec0=new DecimalFormat(“#,###.00”);
double num0;
String string0;
for(int i=0;i Scanner scan0=new Scanner(args[i]);
System.out.println(“The number, formatted, is “+string0+”\n”);
System.out.println(args[i] + ” not a readable number.\n”);
}//end of for loop
System.out.println(“\nSee you again:)\n\n”);
}//end of main
}//end of class

Let’s imagine you save the above program as, then compile it as follows:


Finally, you run it with the following command call:

java DecFormatEx 1139.726 23 0.113e1 2000000.29

You’ll hopefully receive the output

The number, formatted, is 1,139.73
The number, formatted, is 23.00
The number, formatted, is 1.13
The number, formatted, is 2,000,000.29

As usual, this little Java program needs more explanation than fits in one post. I’ll be continuing about it tomorrow.



Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

chmod: Setting a file read-only in Linux

The tutor uses Linux and Windows both, so he tries to cover them equally.

In yesterday’s post I explained a way to set a file read-only in Windows. Today, I do so for a Linux system.

Linux has many flavours; I use Ubuntu. I’m told that some more elite Linux users find Ubuntu “too easy to use – anyone can use it.” (I’m sure the Ubuntu developers are only too happy to hear such criticism.)

Although Ubuntu has a very user-friendly desktop, it also has the terminal. The highbrow might be more comfortable there. It’s the context I’m using today, because so far as I know, the terminal is common to all Linux users.

Let’s imagine you’ve just finished a program called prog0.txt, which you’ve saved in your scripts directory. You’ve tested it and you’re very pleased. Naturally, you want to safeguard it from impetuous changes.

Here’s how you can set it read-only:

  1. For simplicity’s sake, close the file first.
  2. In the terminal, navigate yourself into the scripts directory.
  3. Type the command chmod 444 prog0.txt

On Linux, the text editor I use is gedit. When I open a read-only file in gedit, no indication appears that the file is read only. However, when I try to make a change to the file, a read-only message appears across the top. If I try to save any changes, I am forced to save them under a different file name.

Besides this simple use, the chmod command has other capabilities. I’ll be covering some of them in future posts:)


McGrath, Mike. Linux in easy steps. Southam: Computer Step, 2008.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

How to make files read-only in Windows

The tutor continues his discussion of read-only files.

In yesterday’s post I opened the discussion about read-only files – specifically, why one might want to use them.

Suppose one did want to use them. How would they go about doing so, on Windows?

Let’s imagine you’ve just finished a computer program whose file name is prototype.txt. You’ve tested it and you’re satisfied it finally does exactly what you want. You don’t want it changed.

To set the file read-only, there is more than one way. Today I’ll describe the method most people will likely prefer; i.e., using the desktop:

  1. In Windows Explorer, find the file.
  2. Select the file and right-click it.
  3. Click Properties (it’s at the bottom).
  4. At the bottom of the Properties dialogue, notice Attributes.
  5. Click the Read-only box.
  6. Click Apply, then OK.

I find I can only do this successfully when the file is closed.

My experience is that, once I’ve done this to a Word document, then I open the document, it says Read-Only at the top. By contrast, when I open a read-only file in Notepad, there seems no indication that the file is read-only. However, when I make a change to the file, then try to save it, I’m finally told that the file is read-only. The dialogue box explains that, to save the changes, I need to call the file a different name.

Tomorrow, perhaps a method for Linux.


Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Academic habits: using read-only files

The tutor shares a habit he recently began.

Let’s imagine writing a large essay or computer program, then reaching a point of satisfaction.  You save it to disk, then contentedly go off to celebrate with a visit to a friend, a TV show, or what have you.

A few hours later, a new idea materializes.  While still happy with your project, you’re excitedly thinking that the change you’ve newly conceived will take it to the “next level.” You end off celebrating, anxious to get back to the computer to improve your project. You open the file and start changing it….

So many of us have lived the above scenario, only to realize that the new idea doesn’t work because of something we failed to recall.  Now the project is half-changed, but needs to be changed back.  If we’ve been saving as we go (which, generally, is the way we’ve been trained), it’s too late; reconstructing the original project is likely impossible. Even if we can rewrite it very similarly – often an exercise in self-deprecatory frustration  – we know, at the end, it’s not so shiny and neat as before.

For me – not anymore!  I’ve begun the habit of setting my completed work “read-only”. In order to rework a piece set that way, I just make a copy of it to a new file, then start reworking the copy.  If the rebuild goes bad, no problem; the original is safe.

Without marking the original “read-only”, a person could just make a copy of it and start changing the copy, leaving the original intact. Yes, they could, but if they’re excited and confident about the changes they want to make, they just might not take that precaution.  The “read-only” setting on the file – though it can be changed – reminds me of why it’s there.  Not wanting to reverse what I’ve done earlier for my own protection, I just copy the file and start changing the copy.

I’ll be discussing how to set files “read-only” on both Windows and Linux in coming posts:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Lifestyle: dandelion greens, part one

The tutor shares his long-standing affinity for this perennial weed.

I don’t see “weeds” the same way many others do.  Land recently cleared, then left unused, tends to sprout scenic collages that fascinate me.  The widely differing colors and structures, abundant with energy and purpose, I find much more inspiring than a uniform lawn with some neatly planted flowers.

Yes, I have a lawn, which I do try to keep weed-free.  (I never use chemicals to do so, just elbow grease.)  However, the fringes of my yard – because of its odd shape, it has many – I’m happy to leave to the weeds, at least for a month or two at a time.

One weed I’m always pleased to see in my yard is the dandelion.  From the lawn, I pull it up, then drop it for mulch.  In the less-traveled parts of the yard I let it flourish, because I eat the greens (not the flowers).

As a kid I recall hearing the importance of green, leafy vegetables.  I hold up dandelion greens as a great example.  From March through October, they are a staple of my vegetable intake.

A caution for anyone tempted to eat dandelion greens from a yard: make sure no pesticides have been used there.  If you can’t be sure, you’re probably best off not to take the chance.

Dandelion greens offer a wide range of potential health benefits, which I’ll discuss in future posts:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Windows: my adventure backing up Windows Live Mail messages

The tutor relates some highlights that transpired as he recently backed up the email.

I’m no expert on Windows, but I have to maintain it in our home.  Of course, that means making back-ups of important records – including email messages.

Well, I read how to back up Windows Live Mail messages from the people at microsoft. I confess that the procedure didn’t work for me when trying to send the messages to a flash drive; however, I was able to export them to a folder on the desktop. Yet, the point of backing up files means you wouldn’t likely store the backup on the same computer whence the files originate. I guess you might do so if the computer has two hard drives; mine doesn’t.

So then I wondered: could I just copy the exported emails from their folder on the desktop to a USB drive?

The idea seemed to make sense – except that the folder holds 1.6 GB of data, and the transfer speed was reporting to be (no lie) under 10 kilobytes/second. At that rate, the copy to the flash drive would take more than 44 hours. What was I doing wrong?

I’d read somewhere, I think on a Microsoft message board, that data transfer between different file systems can be slow. Of course, my Windows 7 hard drive uses NTFS format; what about my USB drives? I checked them all: FAT32, every single one.

Then I wondered – could I reformat one of the flash drives to NTFS? said it could be done. I moved some files off a USB drive that was nearly empty, then reformatted it to NTFS. Really, it couldn’t have been easier. (One should NEVER reformat a drive without very careful thought. All the current files on the drive must be saved elsewhere before the reformat to avoid losing them.)

With the USB drive freshly converted to NTFS, I once again tried exporting the email messages to it from Windows Live Mail. This time it worked; the transfer rate was just over 1MB/s. The transfer took around 25 minutes.

At the end, I checked the folder on the flash drive to which I’d (hopefully) exported the backed up email messages. There it was: all 1.6GB!

That’s how I exported email messages from Windows Live Mail to a flash drive. I don’t really know if it worked unless/until I try to import them. But of course, that’s sometimes the case with backups, isn’t it? Furthermore, one hopes to never have to find out:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Lifestyle: spring and yard work

The tutor switches gears to discuss signs and consequences of spring.

As of last Friday, it’s officially spring. Back in my post on January 26, I acknowledged that spring may not come simultaneously to all Canadians. If it hasn’t reached you yet, hang in there.

On February 23 I reflected on the beautiful weekend we’d enjoyed Feb 21 and 22. Now, during our first weekend of official spring, the weather isn’t as clement as then. I’m not complaining; it’s 9°C, mainly cloudy, but still pleasant if you’re out doing chores. (Yesterday was sunnier, 12°C.)

Weekends can be sunnier or cloudier, warmer or cooler; yard chores continue withal. Being outside, one sees signs that confirm it really is spring, regardless of the weather this moment. Last night I heard the frogs booming loudly from a park nearby. Today a flock of geese flew over, heading due north. Bushes are sprouting leaves and spiders are everywhere (large ones, too)!

Yard chores, like signs of spring, sometimes require a person’s proximity in order to be noticed. One would never see the geese fly over, were one not outside. Similarly, one might not notice the weeds in the lawn from inside the house. Being outside, one begins to realize the progress – both of the season, and of the weeds. It can be reassuring and daunting at the same time.

To those who aren’t yet feeling spring: I’m just as certain it’s coming as I am that I need to tend my lawn. Likely, you’ll soon be in my boat; take heart as the snow melts:)

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.