• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

format decimal number  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks, I need your help

I have to format a number exactly as does a Visual Basic function, and not how to do it in Java.

Visual Basic:

number = "0123456789123456"
RSet number = Format(123456789, "############0.000")
el resultado es: 123456789,000

number = "0123456789123456"
RSet number = Format(123456789123, "############0.000")
result: 123456789123,000

number = "0123456789123456"
RSet number = Format(1234567891234, "############0.000")
result: 1234567891234,00

number = "0123456789123456"
RSet number = Format(12345678912345, "############0.000")
result: 12345678912345,0

number = "0123456789123456"
RSet number = Format(1234567891234567, "############0.000")
result: 1234567891234570

It depends on a maximum length of characters and finally rounding the number.
Not how to do it in java ...
 
Marshal
Posts: 60912
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Are those integers or floating‑point numbers? What you are writing looks more like Strings. You cannot use the same syntax in Java® as you would in VB. If you have numbers, start reading in the Java™ Tutorials: I suggest two sections: 1 2. Use the % tags, not a decimal format object.

If you need to turn those Strings into numbers, try this sort of thing:-
BigInteger bi = new BigInteger("0123456789123456");
Don't write integer literals starting with 0; that would turn them into octal.
If you need to turn those whole numbers into decimal fractions with ….000 at the end, you might be able to turn them to BigDecimals and prints them with three places after the decimal point.

Please explain more about what sort of rounding you want.
 
wagego quemero
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I have to do is format that bring true value of a SQL query

number = "0123456789123456"
RSet number = Format (matrizordenes ("cantf"), "############ 0.000")
msgbox (number)

matrizordenes ("cantf") = value returned by an SQL query
 
Campbell Ritchie
Marshal
Posts: 60912
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many digits? How many after the decimal point? What format are they in the database?
 
wagego quemero
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:How many digits? How many after the decimal point? What format are they in the database?



according to the visual basic code a maximum of 16 digits (13 digits integers and 3 decimals).
In the database is String.

Now, if the value is less than 13 digits, decimals appear. If the value has more than 13 digits will be removing decimals and rounding.

val = "0123456789123456" (16 digits)

RSet number = Format (123456789, "############ 0.000") (9 digits)
result "123456789.000"

RSet number = Format (1234567891234, "############ 0.000") (13 digits)
result: 1234567891234.00

RSet number = Format (12345678912345, "############ 0.000") (14 digits)
result: 12,345,678,912,345.0

RSet number = Format (1234567891234567, "############ 0.000") (16 digits)
result: 1234567891234570 (rounding here and everything, no decimal places)
 
Campbell Ritchie
Marshal
Posts: 60912
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about the VB code. How many digits do you want to display? What format is the number in the database? Do you want up to 13 digits before the decimal point and always 3 digits after?
What happens if your number is 123456789012345.678?
That has 15 digits before the decimal point.
And how is the number stored in the database? I hope not as a String/VARCHAR.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!