Jtest logo




Contents  Previous  Next  Index

INTER.NTS


A number variable should not be followed by 'toString()' in an internationalized environment

Description

This rule flags number variables that are followed by `toString()'.

If code contains number variables that are followed by `toString()', it will not run in an internationalized environment because it will not be displayed correctly in all countries. If code displays numbers and currencies, they must be formatted in a locale-independent manner.

Example

 package INTER;
 import java.awt.*;
 public class NTS{
   public void displaynum(){
      Double amount = new Double(3.2);
      String displayAmount = amount.toString();  //violation.
      System.out.println(displayAmount);
   }
 } 

Repair

Replace the preceding code with a routine that formats the number correctly. The Java programming language provides several classes that format numbers. One way to format numbers is by using predefined formats. This can be done by using the "NumberFormat" class which allows you to format numbers, currencies, and percentages according to Locale. The following code is an example of formatting a Double according to Locale.

  
 Double amount = new Double(3.2);
 NumberFormat numberFormatter;
 String amountOut;
 
 numberFormatter = NumberFormat.getNumberInstance(currentLo-
cale);
 amountOut = numberFormatter.format(amount);
 System.out.println(amountOut + "  " + currentLo-
cale.toString());
 

Reference

http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html

http://java.sun.com/docs/books/tutorial/i18n/format/numberFormat.html


Contents  Previous  Next  Index

ParaSoft logo
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft