将字符串构建为格式,然后将其用作格式

时间:2016-02-15 08:23:23

标签: vb.net string format

有没有办法将字符串用作IFormatProvider。

有些事情如下:

Dim MyValue As String = "1216,00"
Dim MyDecimalSymbol As String = "."
Dim Format As String = "00" & MyDecimalSymbol & Decimals
MyValue = MyValue.ToString(Format)
'Output should be 1216.00

可悲的是,该功能不接受字符串,我必须动态构建它,有没有办法做到这一点?

3 个答案:

答案 0 :(得分:0)

你喜欢这样做:

    Dim MyValue As Double = 1216.01
    Dim MyDecimalSymbol As String = "."
    Dim Decimals As String = "00"
    Dim Format As String = "00" & MyDecimalSymbol & Decimals
    Dim myStringValue As String
    myStringValue = MyValue.ToString(Format)
    Console.WriteLine(myStringValue)
    Console.ReadLine()

但是,您可能无法正常工作

小数点“。”将被您的货币设置所取代(因此它可以在美国和其他国家/地区使用,但在某些欧洲国家/地区无效。)

答案 1 :(得分:0)

这样的东西会用当前的文化小数分隔符替换逗号并根据需要格式化值,但是没有检查以确保值首先在其中包含逗号,因此您可能需要添加的是:

    Dim MyValue As String = "1216,00"
    Dim MyDecimalSymbol As String = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator
    Dim decimals = "00"
    MyValue = String.Format("{0:00}{1}{2}", MyValue.Split(","c)(0), MyDecimalSymbol, CInt(MyValue.Split(","c)(1)).ToString(decimals))

答案 2 :(得分:-1)

好的,谢谢大家的建议,我用不同的方式实现我想要的,就是这样:

#This is a configuration file for automatic starting of the Oracle
#Database and listener at system startup.It is generated By running
#'/etc/init.d/oracle-xe configure'.Please use that method to modify this 
#file

# ORACLE_DBENABLED:'true' means to load the Database at system boot.
ORACLE_DBENABLED=true

# LISTENER_PORT: Database listener
LISTENER_PORT=1521

# HTTP_PORT : HTTP port for Oracle Application Express
HTTP_PORT=8080

# Configuration : Check whether configure has been done or not
CONFIGURE_RUN=true
相关问题