How to remove zeroes after the decimal point as an expression in SSRS - vba

I have the following expression in one of my textbox in my SSRS Report:
=IIF(IsNothing(Lookup(Trim(Fields!venta_Cod_vendedor.Value) & "-" & ReportItems!Textbox233.Value, Fields!AgregarVentas.Value, Fields!venta_prom_sem.Value, "EfectividadDeFrecuencias_Ventas")) = True
,"0"
,IIF(Lookup(Trim(Fields!venta_Cod_vendedor.Value) & "-" & ReportItems!Textbox233.Value, Fields!Agregar.Value, Fields!total_cant_pos.Value, "EfectividadDeFrecuencias_Total") <> "0"
,FormatNumber(Lookup(Trim(Fields!venta_Cod_vendedor.Value) & "-" & ReportItems!Textbox233.Value, Fields!AgregarVentas.Value, Fields!venta_frecuencia.Value, "EfectividadDeFrecuencias_Ventas")
/ Lookup(Trim(Fields!venta_Cod_vendedor.Value) & "-" & ReportItems!Textbox233.Value, Fields!Agregar.Value, Fields!total_cant_pos.Value, "EfectividadDeFrecuencias_Total"),2)
,"0"))
That division will give me an int64 number, 15 digits (If such math operation gives that amount of decimal digits).
So the results are:
Now here is the tricky part:
My code behind that grabs the Dataset does a round and converts to decimal and then shows to a Crystal Report.
dr.venta_prom_sem = (Convert.ToDouble(dr.total_cant_pos) != 0 ? (Math.Round((Convert.ToDouble(dr.venta_frecuencia) / Convert.ToDouble(dr.total_cant_pos)), 2)).ToString() : "0");
So this will give me:
as you can see if I use a format Number the 1.3 will convert to 1,30 and that will be wrong, same as 1 (1,00). Now 1,339...etc will give me 1,34 and that is fine.
But check the 1.065, with FormatNumber that will give me 1.07 instead of 1.06.
So the thing is, how can I format my numbers to be the last non zero digit after the decimal point AND select the lower value if the (in this case) 3rd value is 5, instead of 1.07 be 1.06. I think If I use Ceiling or Floor it gives me the integer part.

Try this:
=ROUND(1.339,2,MidpointRounding.ToEven)
This gives: 1.34
And
=ROUND(1.065,2,MidpointRounding.ToEven)
Gives: 1.06
Let me know if this was helpful.

Related

Formatting a double variable using String Format to add up to 3 zero from the right

I got a price decimal which sometimes can be either 0.00002001 or 0.00002.
I want to display always 3 zeros from the right if the number is like 0.00002 so I'm looking it to be 0.00002000. If the number is 0.00002001 do not add anything.
I came accross some examples and other examplesin msdn and tried with
price.ToString.Format("{0:F4}", price)
but It doesn't actually change anything in the number.
And in the case number is like 123456789 I want it to display 123.456.789 which I've half solved using ToString("N2") but it's displaying also a .00 decimals which I don't want.
Some special cases here between the fractional and whole numbers, so they need to be handled differently.
Private Function formatWithTrailingZeros(number As Double) As String
If number Mod 1 > 0 Then ' has a fractional component
Return $"{number:0.00000000}"
Else
Dim formattedString = $"{number:N2}"
Return formattedString.Substring(0, formattedString.Length - 3)
End If
End Function
Dim price = 0.00002001
Console.WriteLine(formatWithTrailingZeros(price))
price = 0.00002
Console.WriteLine(formatWithTrailingZeros(price))
price = 123456789
Console.WriteLine(formatWithTrailingZeros(price))
price = 123456789.012345
Console.WriteLine(formatWithTrailingZeros(price))
0.00002001
0.00002000
123,456,789
123456789.01234500
If your second case with 123.456.789 is not based on your current culture, then you may need to replace , with . such as
Return formattedString.Substring(0, formattedString.Length - 3).Replace(",", ".")
Since you are using . both as a decimal separator and a thousands separator, I'm not sure how my example of 123456789.012345000 should look, but since you didn't ask, I'm not going to guess.

Format percentage with or without decimal

I have a very simple issue in MS-Access and somehow the solution eludes me. I want to display a field that holds a percentage with or without decimals. So I want to display the decimal separator only when there is actually a decimal in the field. This illustrates the problem:
debug.? format(0.21, "0.#%"), format(0.215, "0.#%")
21,% 21,5%
How to get rid of the nasty comma in 21,%. I tired al sorts of format options. I either always get a decimal or I get the value rounded, which I do not want.
How can I display 0.21 as 21% and 0,215 as 21,5% ?
You can use IIf:
PercentValue = Format(Value, "0" & IIf(Value * 100 = Fix(Value * 100), "", ".##") & "%")
Value = 0.21 -> 21%
Value = 0.215 -> 21.5%

FormatNumber replacing number with 0

Not understanding this:
Number returned from DataReader: 185549633.66000035
We have a requirement to maintain the number of decimal places per a User Choice.
For example: maintain 7 places.
We are using:
FormatNumber(dr.Item("Field"), 7, TriState.false, , TriState.True)
The result is: 185,549,633.6600000.
We would like to maintain the 3 (or 35) at the end.
When subtracting two numbers from the resulting query we are getting a delta but trying to show these two numbers out to 6,7,8 digits is not working thus indicating a false delta to the user.
Any advice would be appreciated.
Based on my testing, you must be working with Double values rather than Decimal. Not surprisingly, the solution to your problem can be found in the documentation.
For a start, you should not be using FormatNumber. We're not in VB6 anymore ToTo. To format a number in VB.NET, call ToString on that number. I tested this:
Dim dbl = 185549633.66000035R
Dim dec = 185549633.66000035D
Dim dblString = dbl.ToString("n7")
Dim decString = dec.ToString("n7")
Console.WriteLine(dblString)
Console.WriteLine(decString)
and I saw the behaviour you describe, i.e. the output was:
185,549,633.6600000
185,549,633.6600004
I read the documentation for the Double.ToString method (note that FormatNumber would be calling ToString internally) and this is what it says:
By default, the return value only contains 15 digits of precision although a maximum of 17 digits is maintained internally. If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. If you require more precision, specify format with the "G17" format specification, which always returns 17 digits of precision, or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision.
I then tested this:
Dim dbl = 185549633.66000035R
Dim dblString16 = dbl.ToString("G16")
Dim dblString17 = dbl.ToString("G17")
Console.WriteLine(dblString16)
Console.WriteLine(dblString17)
and the result was:
185549633.6600004
185549633.66000035

in VB.net, if nth digit after decimal is Zero, then Round function will apply for 7 digit

let say n decimal = 0.996010569
I have applied below code (round on 6th digit) :
txtDiscountRate.Text = Math.Round(Val(txtDiscountRate.Text.Trim), 6)
But since here its 6th digit is 0 (Zero), so its value become 0.99601.
But i wish it would be 0.996011.
logic is: if 6th digit is 0 or < 5 then it do Round from 7th digit
then our calculation will be right.
Please provide Code in VB.net.
Jerry
in above code.
CInt(Str(1).Char(5)) is showing error this error -----> " 'Char' is not a member of 'String'".
Scenario is described below:
in txtDiscountRate.text have value "0.996010500406591" .
in my coding i did
txtDiscountRate.Text = Math.Round(Val(txtDiscountRate.Text.Trim), 6) . (means considering round till digit throught)
so it giving value 0.99601 which is because of 6th digit after decimal is 0,
but i want to put condition, in decimal value, if on 6th digit after decimal is ( 0 or 1 or 2 or 3 or 4 ) and 7th digit after decimal is available then it round till 7th position.
else it round till 6th position.
Use the FormatNumber() function.
Maybe not the best and prettiest solution, but you can give it a try...
Dim Dec As Decimal = 0.996010569
Dim str() As String = Split(CStr(Dec), ".")
If CInt(Str(1).Chars(5)) < 5 Then 'It's Char number 5 since it's a zero-based index. So the first number = Index 0.
txtDiscountRate.Text = Math.Round(Val(txtDiscountRate.Text.Trim), 7)
Else
txtDiscountRate.Text = Math.Round(Val(txtDiscountRate.Text.Trim), 6)
End If

vb.net what is a good way of displaying a decimal with a given maximum length

I am writing a custom totaling method for a grid view. I am totaling fairly large numbers so I'd like to use a decimal to get the total. The problem is I need to control the maximum length of the total number. To solve this problem I started using float but it doesn't seem to support large enough numbers, I get this in the totals column(1.551538E+07). So is there some formating string I can use in .ToString() to guarentee that I never get more then X characters in the total field? Keep in mind I'm totaling integers and decimals.
If you're fine with all numbers displaying in scientific notation, you could go with "E[numberOfDecimalPlaces]" as your format string.
For example, if you want to cap your strings at, say, 12 characters, then, accounting for the one character for the decimal point and five characters needed to display the exponential part, you could do:
Function FormatDecimal(ByVal value As Decimal) As String
If value >= 0D Then
Return value.ToString("E5")
Else
' negative sign eats up another character '
Return value.ToString("E4")
End If
End Function
Here's a simple demo of this function:
Dim d(5) As Decimal
d(0) = 1.203D
d(1) = 0D
d(2) = 1231234789.432412341239873D
d(3) = 33.3218403820498320498320498234D
d(4) = -0.314453908342094D
d(5) = 000032131231285432940D
For Each value As Decimal in d
Console.WriteLine(FormatDecimal(value))
Next
Output:
1.20300E+000
0.00000E+000
1.23123E+009
3.33218E+001
-3.1445E-001
3.21312E+016
You could use Decimal.Round, but I don't understand the exact question, it sounds like you're saying that if the total adds up to 12345.67, you might only want to show 4 digits and would then show 2345 or do you just mean that you want to remove the decimals?