SSRS Format Custom Number - sql

I have a report that I am running that will end up with values like this
100.00
99.98
98.80
100.00
I have a custom format set up where I want to remove the decimals after the 100s and keep all other decimal places. Here is my expression
=IIF(Fields!Field1.Value Is "100.00", "100", Fields!Field1.Value)
This works for the 100 but it removes the 0 after 98.80. Here is what I end up with.
100
99.98
98.8
100
Is there a way to not remove that single trailing 0?

I would leav ethe value expression alone, just let that be the field value.
I would do this in the format expression of the cell/textbox instead. So the format expression would be something like.
=IIF(Fields!Field1.Value = 100, "f0", "f2")
This assumes your data is numeric.

=IIF(Fields!Field1.Value Is "100.00", "100", Format(Fields!Field1.Value,"##.##"))

Related

Coverting Float to String with comma without Rounding

I'm using this for adding commas into number.
val commaNumber = NumberFormat.getNumberInstance(Locale.US).format(floatValue)
floatValue is 8.1E-7 , but commaNumber shows just 0.
How to covert Float to String with comma without Rounding?
For the US locale, the maximumFractionalDigits of the number format is 3, therefore, it will try to format 8.1e-7 with only 3 fractional digits, which makes it 0.000. Since the minimumFractionalDigits is also 0, it tries to remove all the unnecessary 0s, making the final result "0".
You should set maximumFractionalDigits to at least 7 if you want to precisely display the number 8.1e-7.
val numberFormat = NumberFormat.getNumberInstance(Locale.US)
numberFormat.maximumFractionDigits = 7
val commaNumber = numberFormat.format(floatValue)
As for the commas, that is already part of the US locale. It uses grouping, and , is its grouping separator. The commas will be inserted if they are needed.

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%

How to format integer as string with 2 digits?

I would like to format an integer 9 to "09" and 25 to "25".
How can this be done?
You can use either of these options:
The "0" Custom Specifier
value.ToString("00")
String.Format("{0:00}", value)
The Decimal ("D") Standard Format Specifier
value.ToString("D2")
String.Format("{0:D2}", value)
For more information:
Custom Numeric Format Strings
Standard Numeric Format Strings
If its just leading zero's that you want, you can use this:
value.tostring.padleft("0",2)
value.ToString().PadLeft(2, '0'); // C#
If you have 2 digits, say 25 for example, you will get "25" back....if you have just one digit, say 9 for example, you will get "09"....It is worth noting that this gives you a string back, and not an integer, so you may need to cast this later on in your code.
String formate is the best way to do that. It's will only add leading zero for a single length. 9 to "09" and 25 to "25".
String.format("%02d", value)
Bonus:
If you want to add multiple leading zero 9 to "0009" and 1000 to "1000". That's means you want a string for 4 indexes so the condition will be %04d.
String.format("%04d", value)
I don't know the exact syntax. But in any language, it would look like this.
a = 9
aString =""
if a < 10 then
aString="0" + a
else
aString = "" + a
end if

Format a number to display a comma when larger than a thousand

I am writing some code in Visual Basic.net and have a question.
If I have a long number, that is larger than 1000, how can I format this value to be 1,000 (with a comma) and for this to be stored in a string?
For e.g.
1234 will be stored as 1,234
12345 will be stored as 12,345
123456 will be stored as 123,456
Is this done with a TryParse statement?
May I have some help to so this?
Take a look at The Numeric ("N") Format Specifier
General use:
Dim dblValue As Double = -12445.6789
Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
' Displays -12,445.68
If you are only using integers then the following:
Dim numberString As String = 1234.ToString("N0")
Will show numberString = "1,234" as the "N0" format will not add any figures after a decimal point.
For those wanting to do a currency with commas and decimals use the following: .ToString("$0,00.00")
Using $ notation:
int myvar = 12345;
Console.WriteLine($"Here is my number: {myvar:N0}");