0

I have a range of numbers and that will be strings 1.1 , 1.2, 1.10, 1.11 but anything under 1.10 comes in as 1.1. I actually need to some how check if the decimal is 1 digit, then add a leading 0.. so it would be 1.01.

number.tostring("F2")

adds a 0 after but how can i get it before?

Sirus
  • 382
  • 1
  • 8
  • 35
  • 1
    [Standard Numeric Format Strings](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings) – Ňɏssa Pøngjǣrdenlarp Aug 15 '17 at 19:43
  • You only need to set this behavior with 1.1 value or there could be more type of these input. Can you post any other example? – M_Idrees Aug 15 '17 at 19:48
  • 2
    Let me understand well. You have a number whose value is 1.1 and you want to output it as "1.01"? Why? – Steve Aug 15 '17 at 19:48
  • 1.01 is a completely different number than 1.1 or did you mean `01.1`? – Ňɏssa Pøngjǣrdenlarp Aug 15 '17 at 19:51
  • yes... It is because the only way this system generates its numbers is through this order. and i need to link the ids to another system that has normal numbers such as 1.01, 1.02. I realize they are different. but the thing is the system thats generating the numbers goes in the order 1.1, 1.2, till 1.10..1.11.. dont think of it has an actual number.. its just an ID system – Sirus Aug 15 '17 at 19:51
  • It seems that you are not talking about numbers here. It seems that you are using these values as some kind of a numbered list (a summary?) and you want to order them according to a [natural sort order](https://stackoverflow.com/questions/248603/natural-sort-order-in-c-sharp) – Steve Aug 15 '17 at 19:55
  • ya, i guess i should of been clearer.. its an ID system that contains numbers but the actual number isn't relevant – Sirus Aug 15 '17 at 19:59
  • 1
    You may want to consider using your own "numbering" class with a .ToString function. – tgolisch Aug 15 '17 at 20:09

2 Answers2

1

A brute force solution of the problem

Dim number As String
number = "1.1"
Dim maxDigits = 2

Dim p = number.Split(".")
p(1) = $"{New String("0", maxDigits - p(1).Length) & p(1)}"

Console.WriteLine(String.Join(".", p))

If you need more than two digits after the "decimal point" you can change the maxDigits constants to the number of digits required

Steve
  • 213,761
  • 22
  • 232
  • 286
0

I made up my own. Just thought there would be some function out there

Dim temp As String
    Dim split As String()

    For Each row As DataRow In dtReturnTable.Rows
        temp = row.Item("ID")
        split = temp.Split(New Char() {"."})

        If split(1).Length = 1 Then
            split(1) = split(1).PadLeft("2", "0")
        End If

        row.Item("ID") = split(0) & "." & split(1)

    Next
Sirus
  • 382
  • 1
  • 8
  • 35
  • You shouldn't be using one number to represent what is really two numbers. – Andrew Morton Aug 15 '17 at 20:59
  • I can't control the numbering system that this application generates. In this case it doesn't represent two numbers... 1000.1 represents 1000.01. and 1000.10 is just 1000.10. like i explained. don't worry about the why – Sirus Aug 15 '17 at 21:19
  • Incidentally, I see you have used `"2"` instead of `2` as the first parameter to `.PadLeft`, so I suspect that you are not using [Option Strict On](https://stackoverflow.com/q/2454552/1115360). It will help greatly in producing programs which behave as intended. – Andrew Morton Aug 17 '17 at 08:38
  • that was just a mistake.. should be 2 as the int. but yes i see ur point on the option strict – Sirus Aug 17 '17 at 15:30