jstl core formatNumber type percent - jstl

JSTL core formatNumber type percent is remove precision values while rendering in UI.
<fmt:formatNumber value="0.25" type="percent" />
out put : 25%
where formatNumber type percent is multiply input value by 100.
Is there a way to avoid get 0.25% this instead of dividing with 100 ?

Related

How to read percentage as decimal number?

I'm trying to find the decimal value from a percentage that a user inputs.
For example, if a user inputs "15", i will need to do a calculation of 0.15 * number.
I've tried using .to_f, but it returns 15.0:
15.to_f
#=> 15.0
I also tried to add 0. to the beginning of the percentage, but it just returns 0:
15.to_s.rjust(4, "0.").to_i
#=> 0
Divide by 100.0
The easiest way to do what you're trying to do is to divide your input value by a Float (keeping in mind the inherent inaccuracy of floating point values). For example:
percentage = 15
percentage / 100.0
#=> 0.15
One benefit of this approach (among others) is that it can handle fractional percentages just as easily. Consider:
percentage = 15.6
percentage / 100.0
#=> 0.156
If floating point precision isn't sufficient for your use case, then you should consider using Rational or BigDecimal numbers instead of a Float. Your mileage will very much depend on your semantic intent and accuracy requirements.
Caveats
Make sure you have ahold of a valid Integer in the first place. While others might steer you towards String#to_i, a more robust validation is to use Kernel#Integer so that an exception will be raised if the value can't be coerced into a valid Integer. For example:
print "Enter integer: "
percentage = Integer gets
If you enter 15\n then:
percentage.class
#=> Integer
If you enter something that can't be coerced to an Integer, like foo\n, then:
ArgumentError (invalid value for Integer(): "foo\n")
Using String#to_i is much more permissive, and can return 0 when you aren't expecting it, such as when called on nil, an empty string, or alphanumeric values that don't start with an integer. It has other interesting edge cases as well, so it's not always the best option for validating input.
I'm trying to find the amount from a percentage that a user inputs
If you retrieve the input via gets, you typically convert it to a numeric value first, e.g.
percentage = gets.to_i
#=> 15
Ruby is not aware that this 15 is a percentage. And since there's no Percentage class, you have to convert it into one of the existing numeric classes.
15% is equal to the fraction 15/100, the ratio 15:100, or the decimal number 0.15.
If you want the number as a (maybe inexact) Float, you can divide it by 100 via fdiv:
15.fdiv(100)
#=> 0.15
If you prefer a Rational you can use quo: (it might also return an Integer)
15.quo(100)
#=> (3/20)
Or maybe BigDecimal for an arbitrary-precision decimal number:
require 'bigdecimal'
BigDecimal(15) / 100
#=> 0.15e0
BigDecimal also accepts strings, so you could pass the input without prior conversion:
input = gets
BigDecimal(input) / 100
#=> 0.15e0

Custom data format in OBIEE - Showing decimal when double, no decimal when integer

I have measure column, which I am using for pivoting, and I have also used New calculated items. Now the new calculated item is to return data in double format,which is percentage, but the other results is to return data in integer. If the data format of the column is decimal then the measure column, which are integers would show data with decimals (so 2 becomes 2.00), and if i keep it integer then decimals from the percentage column would be removed (so 45.28% becomes 45%).
Can the data format of the column be changed such that when there are decimal, then decimals are returned and when whole numbers, whole numbers are returned (without the .00s)?
Expected Result
A B (A/B)*100
2 6 33.33
Note that A and B are coming from the same column, and the (A/B)*100 is my New Calculated Item.
Criteria tab / Properties / Data format and select "up to 2" in decimal places. This option excludes the ".00" for integer values.

validation on discount between 0 and 0.999 in laravel 5

I need to apply validation on discount filed accept value between 0 and 0.999
like: 0.25, 0.0125, 0.09
I tried
'discount' => 'required|max:0.999'
but got: The discount may not be greater than 0.999 characters.
The max rule looks at the type of the variable being sent to it and applies the appropriate logic. For numbers, it works like you intend - it compares the numerical value.
But for strings, it means that the string may not be longer than the max. In your case, Laravel thinks you're sending a string and tries to validate it as such. Your variables probably aren't 0.25, 0.5, etc., but rather "0.25", "0.5", etc. If you convert them to floats, it should work fine.
If, for instance, your values come directly from forms, they're most likely in string form, not float.
Size is what you need here
The field under validation must have a size matching the given value.
For string data, value corresponds to the number of characters. For
numeric data, value corresponds to a given integer value. For files,
size corresponds to the file size in kilobytes.
'discount' => 'required|size:0.999'
'discount' => 'required|numeric|max:0.999'

Suggest an algorithm/method for finding a proper value

I have a bunch of values, for example: [1,2,14,51,100,103,107,110,300,505,1034].
And I have a pattern values, for example [1,10,20,100,500,1000].
I need to get the best 'suitable' value FROM pattern. In my example it is 100. How can I detect this value?
Example from life. The app has a bunch of distances between user position and some objects. The app also has a preset filter by distance: [1 meter, 10 meters, 20 meters, 100 meters]. I heed to set the filter by default not just to the first value (1 meter in my example), but to the value which match the bunch of distances the best way(100 meter in my example). I need to detect one value.
Thank you for help and any ideas.
I would say create a function like this (this is not real code) :
var ratio1 = 0.66
var ratio2 = 1.5
function Score(currentPatternValue, arrayOfValues)
{
count = 0
for each value in arrayOfValues <br>
if value > ratio1 * currentPatternValue AND value < ratio2 * currentPatternValue<br>
count++<br>
return count
}
then you run this for each value in your pattern values and pick the one with the highest score returned from that function

Math.Round() not working when exporting to excel

In my view which I am exporting to excel, I display the value of this variable
VeryGoodPercentage = subQst.Question.ReponseList.Where(
r => r.ReponseValeur == 4 &&
Model.SelectedGroupContactList.Select(c => c.ContaId).Contains(r.ContaId.Value)
).ToList().Count() * 100
/
denominator;
And I display this way
<td class=xl76 align=right width=69 style='width:52pt'>#Math.Round(VeryGoodPercentage)%</td>
When I display the view without exporting to excel, everything works just fine. But when I open the exported Excel file, I found the value of that variable not rounded.
For examlpe :
In the view : 36%
In the excel file : 0.36
I use this line to generate the file:
Response.AddHeader("Content-Type", "application/vnd.ms-excel");
As stated:
36% and 0.36 should be the same - just excel did not fomat this as a percentage or did indeed interpreted 36 as a percentage - which will result in 0.36 as a number.
I don't see that this is a rounding problem, it is formatting or input or both.
Depending on what values #Math.Round(VeryGoodPercentage) produce - 0.xx or XX.XX - with % sign or without, excel will interpret this differently.
Some examples:
=0.36 will result in a number of 0.36
=36 will result in a number of 36
no suprises so far, but now:
=0.36% will result to 0.0036
=36% will result to 0.36
BUT those were Formulars, now if you would just use F2 on an empty 'default' formatted cell and write into it:
36% -> you would format this as percentage, which would display this as 36%, but if you would format it as 'default', then it would be the number 0.36!
0.36% -> would behave like 36%
hope this clears more than it confuses ;) good luck to you!
edit: and to make this even more fuzzy, in my country . is the symbol for thounsands, so you would have to use , instead.
edit: with VBA you could setup your column or cell to use a specific numberformat like this:
Table1.Columns("A").NumberFormat = "0.00%"
I finally found the solution here
Format HTML table cell so that Excel formats as text?
just add this css param to the tag :
mso-number-format:Percent;

Resources