show decimal or double with zeros - asp.net

i get price values from DB.
now whenever the price is perhaps 5, I want to show 5.00
if its 4.3 it should be 4.30.
how to convert that?
thanks

You can use the string format for decimal to apply this formatting.
YourDecimal.ToString("#,##0.00");
this should show 5.00, and 4.30.
Also it will show 1,234.56 groupings.

What data types do you use to store the price? It's a bad idea to store prices using floating point numbers because of precision issues. A fixed point number like a decimal is a better idea.
Once you're settled on a data type, you can use string formatting to display it correctly. See MSDN.

yourDecimal.ToString("N2") will also do the same

I never wrote a single line in Asp.net
but simple search in google gave me this :
https://web.archive.org/web/20210928015431/http://www.4guysfromrolla.com/aspfaqs/ShowFAQ.asp?FAQID=181
http://msdn.microsoft.com/en-us/library/dwhawy9k%28VS.71%29.aspx

Related

How to format numbers in List&Label (by Combit) as power notation?

I'm using List & Label to display results on a report (floats) and I don't want them to be displayed by large numbers of decimal places like:
0.00005850
but like:
5,85e-05
I would be glad if someone had a hint for me :-) I already tried out the number format settings but nothing suiting there. Excel does this automatically, but is there a way in List&Label at all?
For me, the scientific exponential format works nicely - have you tried this setting?

BoundField double show as many decimal places as needed

I have a double variable assigned to a boundfield in a gridview. There will only ever be a max of 5 decimal places.
The boundfield is formatting the display to a scientific value. I'm assuming because it would default to a general format which gives the most compact of either fixed-point or scientific notation (https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring%28v=vs.110%29.aspx)
How can i format the number to show only as many decimal places as needed, up to 5. And do not show scientfic notation?
i.e.
0.00002 is currently showing as 2E-05, but I would like it to show as 0.00002.
0.002 should show as that, and not 0.00200.
1 should show that, and not 1.00000. etc.
So a format such as {0:N5} would not work. {0:N} defaults to 2 decimal places, and so would not work either.
Thanks for any help.
Thanks for the advice #David W
I ended up working it out anyhow.
Although I couldn't find any documentation on it, you can give it proper custom formats.
Therefore, the following solved my issue;
<asp:BoundField DataField="theField" DataFormatString="{0:0.#####}" />

Weird behaviour on Numeric Stepper decimals

I am making an editor for a field with numbers. I tried a text field, but since it's a Number datatype coming in, it didn't go smoothly -- despite recasting strings as numbers etc.. it kept giving me NaN as the value. So I decided it would be best to go with a numeric stepper.
When I initially loaded it up it would drop all my decimals and only display my numbers as integers. I changed the stepIncrement to 0.1 and now it does show the decimals (a weird requirement imo).. but when I step up it occasionally gives me a value like '17.700000000000003' when I would expect 17.7. All of the numbers in my data have a single decimal place. I know I can write a dataformatter, but it seems like it shouldn't be necessary in this situation.
Is there another way I could deal with this?
You've stumbled upon the compromise of trying to represent decimal numbers in floating point binary formats like IEEE 754. Not all decimal numbers can be exactly represented. You can read up on this issue in great detail here:
http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding
You can use Number.toFixed(fractionDigits:uint) to display to an arbitrary number of decimal places.
You can use the valueFormatFunction which takes the numeric value and formats it to a string. You will need to set explicit widths on your numeric steppers to make they fit though.
in your MXML
<s:NumericStepper valueFormatFunction="stepperFormatter"/>
in your script
protected function stepperFormatter(newValue:Number):String
{
return Math.ceil(newValue).toString()
}

Calculation of two fields in ASP.NET (Visual Basic)

I have two money fields in a SQL database called TotalClaimed and PartialSettlementAmountRecd.
They are declared as Decimals like so:
Public PartialSettlementAmountRecd As Decimal
Public TotalClaimed As Decimal
They both output the repsective amounts perfectly. I need to do a calculation on them, by subtracting PartialSettlementAmountRecd from TotalClaimed. I have tried the following, but it just outputs a random number, not the amount I require.
Dim NewSettAmount As Decimal = (ClaimDetail.TotalClaimed) - (ClaimDetail.PartialSettlementAmountRecd)
Response.Write("New Settlement Amount: £" & NewSettAmount)
Where am I going wrong? Thanks...
shouldn't it be:
Dim NewSettAmount As Decimal = (ClaimDetail.TotalClaimed) - (ClaimDetail.PartialSettlementAmountRecd)
<>
I need to do a calculation on them, by subtracting PartialSettlementAmountRecd from TotalClaimed
(ClaimDetail.PartialSettlementAmountRecd) - (ClaimDetail.TotalClaimed)
Aren't you doing the opposite of what you want? Aren't you subtracting TotalClaimed From PartialSettlementAmountRecd?
I apologise. The random figures I was getting were due to somebody entering a huge number in the Total Claimed field in the database whilst I was testing without telling me! The code actually works now with sensible figures.

dos date/time calculation

I am working on a project that involves converting data into dos date and time. using a hex editor (Hex Workshop) i have looked through the file manually and and found the values I am looking for, however I am unsure how they are calculated. I am told that the int16 value 15430 corresponds to the date 06/02/2010 but i can see no correlation, also the value 15430 corresponds to the time 07:34:12 but i am lost in how it is calculated. any help with these calculations would be very welcomed
You need to look at the bits in those numbers.
See here for details:
http://www.vsft.com/hal/dostime.htm
I know this post is very old but I think the time 07:34:12 corresponds to 15436 (not 15430).

Resources