none
Unable to display currency characters in console window. RRS feed

  • Question

  • Imports System.Globalization
    Imports System.Threading
    
    Module Module1
    
        Sub Main()
    
            Dim value As Double = 123456789.12345
            Dim current As CultureInfo = CultureInfo.CurrentCulture
            Console.WriteLine($"The current culture is {current}")
            Console.WriteLine($"The value under the current culture is: {value.ToString("C")}")
            Console.WriteLine("-------------------------------------------------")
            Dim newCulture As New CultureInfo("fr-FR")
            Thread.CurrentThread.CurrentCulture = newCulture
            Console.WriteLine($"The current culture is {newCulture}")
            Console.WriteLine($"The value under the current culture is: {value.ToString("C")}")
            Console.ReadLine()
        End Sub
    End Module

    Initially the current culture was set to English(India) see pics below. And the currency symbol was initially set to the new Indian rupee symbol (see below).

     

    The problem is that neither the new Indian rupee symbol is getting displayed nor the Euro symbol is getting displayed when the culture is getting changed to "fr-FR". See output below:

    Also if I redo the same exercise by changing the currency symbol for the current culture (en-IN) to "$" through the control panel and if instead of "fr-FR" the new culture is set to "en-US" it works fine. See screenshot below:

    Also everything works fine if I try to use the new Indian rupee symbol and the Euro symbol in an excel sheet. So can someone point out why I am able to display the currency symbols in excel sheet but not when I am running a vb code? The screenshot for the excel sheet is found below:



    In my last post I was suggested o use the following line:

    Console.OutputEncoding = Encoding.Unicode

    But not the currency symbol does not get displayed despite using this extra line of code

    Sougata Ghosh



    • Edited by sougata12 Monday, August 13, 2018 11:47 AM changing header
    Monday, August 13, 2018 7:21 AM

Answers

All replies

  • Check the “Unicode Support for the Console” section: https://msdn.microsoft.com/en-us/library/system.console%28v=vs.110%29.aspx. There is an example that sets the “Lucida Console” font using SetCurrentConsoleFontEx. You can try a similar fragment and also use any other font that contains more symbols.


    • Marked as answer by sougata12 Monday, August 13, 2018 4:27 PM
    Monday, August 13, 2018 7:45 AM
  • Imports System.Globalization
    Imports System.Text
    Imports System.Threading
    Imports System.Runtime.InteropServices
    
    Module Module1
    
        Sub Main()
            Dim fontName As String = "Lucida Console"
            Dim hnd As IntPtr = GetStdHandle(STD_OUTPUT_HANDLE)
            If hnd <> INVALID_HANDLE_VALUE Then
                Dim info As CONSOLE_FONT_INFO_EX = New CONSOLE_FONT_INFO_EX()
                info.cbSize = CUInt(Marshal.SizeOf(info))
                Dim tt As Boolean = False
                ' First determine whether there's already a TrueType font.
                If GetCurrentConsoleFontEx(hnd, False, info) Then
                    tt = (info.FontFamily And TMPF_TRUETYPE) = TMPF_TRUETYPE
                    If tt Then
                        Console.WriteLine("The console already is using a TrueType font.")
                        Console.ReadLine()
                        Return
                    End If
                    ' Set console font to Lucida Console.
                    Dim newInfo As CONSOLE_FONT_INFO_EX = New CONSOLE_FONT_INFO_EX()
                    newInfo.cbSize = CUInt(Marshal.SizeOf(newInfo))
                    newInfo.FontFamily = TMPF_TRUETYPE
                    newInfo.FaceName = fontName
                    ' Get some settings from current font.
                    newInfo.dwFontSize = New COORD(info.dwFontSize.X, info.dwFontSize.Y)
                    newInfo.FontWeight = info.FontWeight
                    SetCurrentConsoleFontEx(hnd, False, newInfo)
                End If
            End If
    
    
    
    
            Dim value As Double = 123456789.12345
            Dim current As CultureInfo = CultureInfo.CurrentCulture
    
            Console.WriteLine($"The current culture is {current}")
            Console.WriteLine($"The value under the curent culture is: {value.ToString("C")}")
            Console.WriteLine("-------------------------------------------------")
            Dim newCulture As New CultureInfo("fr-FR")
            Thread.CurrentThread.CurrentCulture = newCulture
            Console.WriteLine($"The current culture is {newCulture}")
            Console.WriteLine($"The value under the curent culture is: {value.ToString("C")}")
            Console.WriteLine("Current code page: {0}", Console.OutputEncoding.CodePage)
            Console.ReadLine()
    
    
        End Sub
    
        Private Declare Function GetStdHandle Lib "Kernel32" (
                      nStdHandle As Integer) As IntPtr
    
        ' [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        Private Declare Function GetCurrentConsoleFontEx Lib "Kernel32" (
                        consoleOutput As IntPtr,
                        maximumWindow As Boolean,
                        ByRef lpConsoleCurrentFontEx As CONSOLE_FONT_INFO_EX) As Boolean
    
        ' [DllImport("kernel32.dll", SetLastError = true)]
        Private Declare Function SetCurrentConsoleFontEx Lib "Kernel32" (
                        consoleOutput As IntPtr,
                        maximumWindow As Boolean,
                        consoleCurrentFontEx As CONSOLE_FONT_INFO_EX) As Boolean
    
        Private Const STD_OUTPUT_HANDLE As Integer = -11
        Private Const TMPF_TRUETYPE As Integer = 4
        Private Const LF_FACESIZE As Integer = 32
        Private INVALID_HANDLE_VALUE As IntPtr = New IntPtr(-1)
    
    
        <StructLayout(LayoutKind.Sequential)> Friend Structure COORD
            Friend X As Short
            Friend Y As Short
    
            Friend Sub New(x As Short, y As Short)
                Me.X = x
                Me.Y = y
            End Sub
        End Structure
    
        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> Friend Structure CONSOLE_FONT_INFO_EX
            Friend cbSize As UInteger
            Friend nFont As UInteger
            Friend dwFontSize As COORD
            Friend FontFamily As Integer
            Friend FontWeight As Integer
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> Friend FaceName As String
        End Structure
    
    
    End Module
    i did what you asked me to do. But I am getting the output as "The console already is using a true type font". I have given the code that I used. Can you please suggest changes to make it work?

    Sougata Ghosh

    Monday, August 13, 2018 10:29 AM