#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    87
    Rep Power
    10

    Format numbers in vb.net


    Hi all, i was trying to insert a zero for integer numbers for example i would want to display the number 32 as 32.0.

    Actually i want to format the number to 1 decimal point so i did a math.round(value,1) so that there is only 1 decimal, but if the numbers are integers i couldnt see a 0 after a decimal ...


    Is there a function in vb.net to do this .....
  2. #2
  3. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,400
    Rep Power
    1233
    Double.ToString(string format) is probablly what you are looking for
    more info

    In essence
    Code:
    Double x;
    ...
    
    x.ToString(0.0);
    is probablly what you are looking for
    (note C# notation, adjust to VB as necessary)

    -MBirchmeier
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    87
    Rep Power
    10
    these numbers are actually retrieved from database, the initial form is like 00003400 for example. What i did was i did a cdbl(00003400)/100 then i got 34, but i shud then display the number as 34.0 .....

    Thanks for your time .....
  6. #4
  7. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,400
    Rep Power
    1233
    Originally Posted by rockwell
    Thanks for your time .....
    Did my method work(it will work if it was applied right)? Did you find a method that worked? If so can you post it?

    You're posting more information as if you're still looking for a response, but i've seen nothing that you've done, a snipit of code, or a format layout for what you're doing. Please be clear and concice with your information.

    -MBirchmeier
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    87
    Rep Power
    10
    hi heres the snippet of code that i am using

    i have this number 000002300 in the variable var


    Code:
    var = CDbl(var) / 100
    'this yiesld var=23
    var = var.ToString("0.00")
    'should this yield 23.00 if i am not wrong
    if i am wrong can you suggest me how to correct it ....

    Appreciate your help ......
  10. #6
  11. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,400
    Rep Power
    1233
    Originally Posted by rockwell
    hi heres the snippet of code that i am using

    i have this number 000002300 in the variable var


    Code:
    var = CDbl(var) / 100
    'this yiesld var=23
    var = var.ToString("0.00")
    'should this yield 23.00 if i am not wrong
    if i am wrong can you suggest me how to correct it ....

    Appreciate your help ......
    The line:
    Code:
    var = var.ToString("0.00")
    is wrong. You're taking a double, converting it to a string, then storing it back as a double.

    Try changing the sinipit to read
    Code:
    Dim strVar as String
    strVar = var.ToString("0.00")
    strVar should then be in the correct format.

    -MBirchmeier

    (Note: Doubles and numeric types store nothing about string formatting, you either need to convert the format to a string and store that information or convert it before usage.

    Additionally you can mess with culture settings to display trailing zeros by default, but its a bit more difficult)
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    87
    Rep Power
    10
    Thanks for your reply, i got the concept ......
  14. #8
  15. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,400
    Rep Power
    1233
    Originally Posted by rockwell
    Thanks for your reply, i got the concept ......
    ...and....?

    you're not addressing any of the things i'm pointing out... I'm trying to help but getting information is like pulling teeth. It's becoming more frustrating to help you than not.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    87
    Rep Power
    10
    dude i really appreciate your help, i am very new to .net tech and u know i am trying to understand it, the snippet of code which u gave works, u even suggested me with some thing else and im tryin it out, i am not like an experienced pro man, this is not my field .....


    well sorry if i pissed u off .....
  18. #10
  19. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,400
    Rep Power
    1233
    I dont mean to come down on you, its just I have no idea what's going on on your end. I haven't heard if you've gotten things working or not.

    This helps sum up what i'm feeling right now http://forums.devshed.com/showpost.p...86&postcount=1

    In addition to that detailed replies are also nice.

    I tried your suggestion with the following code:
    ...code...
    however I expected 25.00 to print, but 25.0 printed.

    or

    ...code...
    25.00 printed, but I don't understand why...

    or (even better)

    ...code...
    and it did exactly what I wanted!!

    It's really nice to get feedback concerning what's going on. Based on your posts above I didn't know if it was working, if it didn't, or if you still needed help. I tried helping because it still sounded like you were confused.

    As for being very new to .NET, that's fine, I by no means consider myself an expert, and i've been using it almost full time for the past 2 years. I learn things when I help out, and helping out today I learned more about number formatting than I did before, if you want/need help, you also have to be descriptive with feedback.

    -MBirchmeier
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    87
    Rep Power
    10
    yeah i guess i was not decriptive and was not clear about the problem .... sorry about that ....
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0
    Try this to format the number:
    ------------------------------
    Dim dblcst As Double
    dblcst= FormatNumber(dblcst , 2, True, True, True)


    Here the 5 parameters are :
    FormatNumber(value, [trailing digits], [leading digit], [parentheses] ,[group digits])

    Hope that it is enough for your requirement.

IMN logo majestic logo threadwatch logo seochat tools logo