hi i am trying to print after dividing in string builder and printing that string builder let show me my code ,
string.append("Memomry usage:total:"+totalMemory/1024/1024+
"Mb-used:"+usageMemory/1024/1024+
" Mb("+Percentage+"%)-free:"+freeMemory/1024/1024+
" Mb("+Percentagefree+"%)");
in above code "totalmemory" and "freememory" is of double type having bytes value in point not null so i divide it by "1024" two times to get it in "Mb" and "string" is variable of string builder after using this code i am simply printing it a am getting result as shown below,
Used Memory:Memomry usage:
total:13.3125Mb-used:0.22920989990234375Mb (0.017217645063086855%)
-free:13.083290100097656Mb (0.9827823549369131%)
i want to get percentage in twodecimal place and values of used and free memory in mb like this "used:2345.25" in this pattren remember
Hopes for your suggestions
Thanks in Advance
How about String.format()?
System.out.println(String.format("output: %.2f", 123.456));
Output:
output: 123.46
Try like this
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
Using DecimalFormat, we can format the way we wanted to see.
You can use DecimalFormat to print out to two decimal places. So, to print x = 2345.2512 with two decimal places, you would write
NumberFormat f = new DecimalFormat("#.00");
System.out.println(f.format(x));
which will print 2345.25.
Even though it is possible to use NumberFormat and it's subclass DecimalFormat for this issue,
these classes provide a lot of functionality that may not be required for your application.
If the objective is just pretty printing, I would recommend using the format function of the String class. For your specific code it would look like this:
string.append(String.format("Memomry usage:total:%1.2f Mb-used:%1.2f Mb(%1.2f %%)-free:%1.2f Mb(%1.2f %%)",totalMemory/1024/1024,usageMemory/1024/1024,Percentage,freeMemory/1024/1024,Percentagefree));
If you are intending to specify a standard format in which all numbers are represented irrespective of whether they are being parsed from strings or formatted to strings, then I would recommend using singletons of the *Format classes. They allow you to use standard formats and also to pass format descriptions between methods.
Hope that helps you select the right method to use in your application.
Related
I'm trying to format output for user/report appeal, and there are two criteria I'm finding to be in a bit of conflict.
First, the decimal values should line up (format on "%12.10f", predicted integer value range 0-99)
Second, the decimal shouldn't trail an excessive series of zeroes.
For example, I have output that looks like
0.5252772000
0.2053628186
10.5234500000
But using a general formatting, I also end up with:
0.53260000000
0.52630000000
12.43540000000
In certain cases, and it looks kind of garbage.
Is there a simple way to solve this problem? The only solution I can come up with at the moment involves pre-interrogating the data before printing (instead of formatting it during print) which, while technically not expensive, just bugs me as being redundant data handling (ie I have to go through all data once to find the extrema of trailing zeroes to parse against it, and then set the format so that it can go through the data again to parse it)
You can set a DecimalFormat:
DecimalFormat format = new DecimalFormat("0.#");
for (float f : yourFloats){
System.out.println(format.format(f));
}
This also works on doubles.
I am reading a legacy webservice that returns doubles in a format I am not familiar with.
some examples are
1.58e-6
1.56e-6
1.45e-6
They should represent doubles like these (for example)
0.000004343
What is this format and how it can be converted?
You can convert like this
1.58e-6=1.58*10^-6=0.00000158
Ok, it was actually rather simple.
double dbl = 1.45e-6;
DecimalFormat df = new DecimalFormat("#.########");
System.out.println(df.format(dbl));
outputs : 0.00000145
It's called scientific notation. Double.parseDouble() can handle it. NB they're not doubles, they are real numbers expressed in text in scientific notation. Double is the target format you want, not this format.
try this
String s = new DecimalFormat("0.##############").format(Double.parseDouble("1.58e-6"));
Am having an issue with formatting currencies in Java. I am trying to format a number to be printed as currency, with all the decimal points aligned:
£ 1.23
£ 12.34
£123.45
Calling NumberFormat.getCurrencyInstance().format(n) returns a formatted string, but all the numbers are left-aligned, producing output like this:
£1.23
£12.34
£123.45
Ugly. I have read this post which presents a solution using a DecimalFormat, and as a stopgap I'm formatting my number using a DecimalFormat and prepending the currency symbol later, but I was wondering if anyone was aware of a neater way of accomplishing the same thing?
Hope that's all clear, thanks in advance for your help!
You could do:
String currencySymbol = Currency.getInstance(Locale.getDefault()).getSymbol();
System.out.printf("%s%8.2f\n", currencySymbol, 1.23);
System.out.printf("%s%8.2f\n", currencySymbol, 12.34);
System.out.printf("%s%8.2f\n", currencySymbol, 123.45);
Note: this will only work for currencies whose symbols appear before the amount.
Also be alert to the fact that doubles are not suitable for representing currency.
Try this:
final NumberFormat nf = NumberFormat.getCurrencyInstance();
nf.setMinimumIntegerDigits(3);
System.out.println(nf.format(1.23));
System.out.println(nf.format(12.34));
System.out.println(nf.format(123.45));
Hello and thank you in advance for the help.
I am having some trouble formatting using a Java function to mark up a price in HTML.
It seems that, no matter what I do, I cannot insert custom content between the numbers and the decimal (throws Illegal Argument Exception). Is there any known way to achieve the following:
NumberFormat nf = getNumberFormat("'<span class=\"dollars\">'##'</span></span class=\"decimal\">'.'</span></span class=\"cents\">'00'</span>'", locale);
nf.format(number);
Assume locale and number are correctly initialized.
If you look at the docs for DecimalFormat you'll see that they talk about the prefix and the suffix text - but not putting arbitrary text within a number.
It sounds like you should basically write this bit of formatting yourself - possibly using DecimalFormat for each section of the number.
You might consider using String.format(String pattern, Object... arguments). You can pass your simply formatted numbers as arguments.
In my web application I have a version field that take float input values. But when using values like 1000000.1 (or larger) for the version it displays like 1.0E7. i tried several methods in Float wrapper class. but result still the same.
Thanks
Your problem is not in parsing but in formatting of your values. The value is correct and it is represented in java correctly. If you wish to change the format user either String.format() that provides C style formatting or java.text.NumberFormat.
System.out.printf("%f", Float.parseFloat("1.0E7")); outputs 10000000.000000
See http://ideone.com/3o6dO
Note that 1.0E7 is 1000000.0, not 1000000.1.
it probably has to do with your output format because 1.0E7 == 10000000
I think what you are looking for is the String.format(locale, msg, args) method.
Use String.valueOf(floatNumber)
Suggest use double instead of float